This problem is so simple I can feel the RTFM's coming. However, I've been looking at the docs (Inline, Inline-C, Inline-C-Cookbook ) all morning and I can't figure out how to solve this problem.
I want to use inline C, but I don't want to have C code in the same file as my perl code.
(Emacs doesn't like having two languages in one file. In principle this is a matter of convenience, but in practice I'm having to edit my C in one file then copy-paste it into my perl script.)
Here is working perl:
#!/usr/bin/perl
use Inline C => DATA;
use strict;
use warnings;
use List::Util qw(sum);
use feature qw(say);
my @array = (1..10);
say "native perl: ", sum(@array), ", Inline C: ", sum1(\@array);
__END__
__C__
double sum1(AV* array) {
int i;
double sum = 0.0;
for (i=0; i<=av_len(array); i++) {
SV** elem = av_fetch(array, i, 0);
if (elem != NULL)
sum += SvNV(*elem);
}
return sum;
}
(thanks to mobrule for getting me this far.)
I want to move all of the C code (or as much as possible) into a separate header file.
What I can do is put sum1
into a header, and do this:
# same perl as above except now say sum2 instead of sum1
__END__
__C__
#include "sum.h"
double sum2(AV* array) {
sum1(array);
}
This is good enough as I no longer have to edit the C in perl-mode, but I wonder if there isn't a more elegant solution to this problem?
You can put your C code in a separate file and use Inline::bind
to load it at runtime
use Inline;
use File::Slurp;
my $data = read_file('source.c');
Inline->bind(C => $data);
or loading the source code in a BEGIN {}
block to bind it at compile time
my $data;
use File::Slurp;
BEGIN {
$data = read_file('source.c');
}
use Inline C => $data;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With