I have been programming C for a while. Now I need to write a C program that perl can call. It should have the same syntax as the following dummy perl function: take two inputs, both lines (can contain binary characters, even "\ x00"), output a new line.
Of course, the function algorithm will be more complex, so I need to do this in C.
sub dummy { my ($a, $b) = @_; return $a . $b; }
I briefly reviewed SWIG for implementation, but making input / output different from the whole is not easy, I hope someone can give a concrete example.
Thanks in advance.
UPDATE Got a great example from Rob (author of the Inline :: C module in cpan), thanks!
use warnings; use strict; use Devel::Peek; use Inline C => Config => BUILD_NOISY => 1, ; use Inline C => <<'EOC'; SV * foo(SV * in) { SV * ret; STRLEN len; char *tmp = SvPV(in, len); ret = newSVpv(tmp, len); sv_catpvn(ret, tmp, len); return ret; } EOC my $in = 'hello' . "\x00" . 'world'; my $ret = foo($in); Dump($in); print "\n"; Dump ($ret);
perl
packetie
source share