The document discusses various ways to embed Perl in C and C in Perl. It provides an overview of embedding Perl in C using ExtUtils::Embed, B::C, and XS. It also covers embedding C code in Perl using Inline::C and XS. Finally, it demonstrates examples of calling Perl functions from C code and evaluating Perl code from embedded C programs.
3. The XS way...
It cannot be seen, cannot be felt,
Cannot be heard, cannot be smelt.
It lies behind stars and under hills,
And empty holes it fills.
- J.R.R. Tolkien, The Hobbit
Answer: dark.
7. fun way of using Inline::C
$ cat perlsign.pl
#!/usr/bin/perl
use Inline C=>'
void C() {
int m,u,e=0;float l,_,I;
for(;1840e;putchar((++e>907&&942>e?61m:u)
["n)moc.isc@rezneumb(rezneuM drahnreB"]))
for(u=_=l=0;79(m=e%80)&&I*l+_*_<6&&26+
+u;_=2*l*_+e/80*.091,l=I)
I=l*l_*_2+m/27.;
}';
&C
8. mmmmmmmmooooooooooooooooooooooooocccccccccc....is@zrre i.cccccccoooooooooommmmm
mmmmmmoooooooooooooooooooooooccccccccccc.....iiscrr n@csi...ccccccoooooooooommm
mmmmooooooooooooooooooooooccccccccccc....iiiss@n zMesii....cccccoooooooooom
mmooooooooooooooooooooocccccccccc....iisssssc@rn erccsiiiii..ccccooooooooo
moooooooooooooooooocccccccc.......iis@e uMeu r e r@@@ezs..cccoooooooo
oooooooooooooooccccccc.........iiiisc@z e eci..cccooooooo
ooooooooooccccccc..iiiiiiiiiiiiisscz z z@sii.ccccoooooo
oooooccccccccc...iicz@ccccz@ccccccrrr s..cccoooooo
ooocccccccc.....iisc@eb bnneree eci..ccccooooo
occcccccc.....iisccrnb m nci..ccccooooo
ccc....iiiiisss@emee( cii..cccccoooo
iscc@@beremeene( Bernhard Muenzer(bmuenzer@csi.com) ercsi...cccccoooo
c....iiiiiisssc@e rnz esi...cccccoooo
occccccc....iiiscc@rb ( esi..ccccooooo
oocccccccc......iisc@z bneze z@i..ccccooooo
ooooocccccccc....ii@er@@@@nr@cccc@e es..cccoooooo
oooooooooccccccc...@siiiiiiiiisssscnM urcsi..cccoooooo
ooooooooooooooccccccc..........iiiisc@e zsi..cccooooooo
mooooooooooooooooocccccccc.......iiis@ nu er eznri.cccoooooooo
mmoooooooooooooooooooocccccccccc....issc@ccc@@rn er@cssiiiss..cccooooooooo
mmmmoooooooooooooooooooooccccccccccc....iiiisc@z rrsii.....ccccoooooooooom
mmmmmooooooooooooooooooooooocccccccccccc....iis@rz rrcsi....ccccccoooooooooomm
mmmmmmmmoooooooooooooooooooooooocccccccccc....iisceeeusi..cccccccooooooooommmmm
10. Variable Argument Lists
greet(qw(Sarathy Jan Sparky Murray Mike));
use Inline C => <<'END_OF_C_CODE';
void greet(SV* name1, ...) {
Inline_Stack_Vars;
int i;
for (i = 0; i < Inline_Stack_Items; i++)
printf("Hello %s!n",
SvPV(Inline_Stack_Item(i), PL_na));
Inline_Stack_Void;
}
END_OF_C_CODE
11. Another way of using Inline::C
use Inline C;
$vp = string_scan($text); # call our function
__END__
__C__
/*
* Our C code goes here
*/
int string_scan(char* str) {
}
13. How you can do it?
● ExtUtils::Embed
perl -MExtUtils::Embed -e ccopts -e ldopts
● B::C
● The only way... XS
14. Docs
man perlembed
man perlcall
man perlguts
man perlapi
man perlxs
15. B::C/perlcc
● Works with Perl 5.6.0
● Works with Perl 5.13.x
#!/usr/bin/perl
use strict;
use warnings;
use lib '.';
use parse_config;
my $a = 'some text';
my $b = 13;
my %config = parse_config('/etc/guardian.conf');
#my %config = ( df => 13, hj => 18);
printf "%s %dn", $a, $b;
while (my ($k,$v) = each %config) {
print "$k :: $vn";
}
16. $ perlcc -o em em.pl
$ ./em
Segmentation fault
$ perlcc -o em em.pl
$ ./em
some text 13
df :: 13
hj :: 18
19. $ ./embed --version
This is perl, v5.10.1 (*) built for i386-linux-thread-multi
Copyright 1987-2009, Larry Wall
Perl may be copied only under the terms of either the Artistic
License or the
GNU General Public License, which may be found in the Perl 5
source kit.
Complete documentation for Perl, including FAQ lists, should be
found on
this system using "man perl" or "perldoc perl". If you have
access to the
Internet, point your browser at http://www.perl.org/, the Perl
Home Page.
21. call_argv("showtime", G_DISCARD | G_NOARGS, args);
vs.
eval_pv(perlPlain, TRUE);
/** Treat $a as an integer **/
eval_pv("$a = 3; $a **= 2", TRUE);
printf("a = %dn", SvIV(get_sv("a", 0)));
/** Treat $a as a float **/
eval_pv("$a = 3.14; $a **= 2", TRUE);
printf("a = %fn", SvNV(get_sv("a", 0)));
/** Treat $a as a string **/
eval_pv("$a = 'rekcaH lreP rehtonA tsuJ'; $a = reverse($a);",
TRUE);
printf("a = %sn", SvPV_nolen(get_sv("a", 0)));
Hinweis der Redaktion
Why would I want to do that? Get some performance by using native C for some of the processing Embed parts of your existing C application into your Perl app without rewriting it completely
XS is an interface description file format used to create an extension interface between Perl and C code extremely hard to learn even the smallest program must be implemented as additional module There is another, similar way using SWIG... I don't know how it works :( If someone is interested... www.swig.org
.
Scalar, Hash and Arrays are typedefs which can include any of the following types. You create the value with the coresponding newSV* function. You access the value with the coresponding Sv* function.
Describe the way we add and execute the C code.
Describe the way we add and execute the code. We begin the function with Inline_Stack_Vars This defines some internal variables including Inline_Stack_Items Inline_Stack_* variables can be used only when we use ... in the argument list or the return type of the function is VOID. sv_2mortal() – marks a variable as ready for destroy newSViv() - creates a Scalar Value from integer
We begin the function with Inline_Stack_Vars This defines some internal variables including Inline_Stack_Items Inline_Stack_* variables can be used only when we use ... in the argument list or the return type of the function is VOID. We have to have at least one argument before the variable length argument because of the XS parsing.
We begin the function with Inline_Stack_Vars This defines some internal variables including Inline_Stack_Items Inline_Stack_* variables can be used only when we use ... in the argument list or the return type of the function is VOID. We have to have at least one argument before the variable length argument because of the XS parsing.
Why would I want to do that? Use Perl's RE Package your software into a single binary Use some of the nice Perl already working perl modules in your C application
In all cases ExtUtils::Embed will help with the compilation flags. B::C is used for direct compile (perlcc) but is available only for perl 5.8.0 or 5.13.x. Using XS seams the only portable/compatible way...
it should work with perl >=5.10 but i haven't made it to work :(