Embed--Basic PERL XS


Published on

Ever wanted to get C to run in Perl. Well here is one place to start

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Embed--Basic PERL XS

  1. 1. Perl XS By John Scoles The Pythian Group Black Art? Your Handy Friend ? Misunderstood Brain Sucking Zombie from Hell?
  2. 2. My XS Story! The funny thing is the Chinese use the same word for catastrophe as they use for opportunity
  3. 3. In The Beginning <ul><li>1800+ line Makefile.PL </li></ul><ul><li>1000+ lines of XS </li></ul><ul><li>800+ lines of Perl </li></ul><ul><li>5300+ lines of pure 'C' </li></ul>
  4. 4. My 'C' Background <ul><li>I can Spell 'c' correctly 87.654% of the time 18 times out of 20. </li></ul>
  5. 5. My XS Background at the time <ul><li>Well I know of them! </li></ul>Bart Simpson said it best when asked if he knew long division or multiplication
  6. 6. A Complete <ul><li>FNG </li></ul><ul><li>NEWBIE </li></ul><ul><li>RETARD </li></ul><ul><li>Whatever </li></ul>
  7. 7. So as Johny Cash would say <ul><li>I Fell into a Burning Ring of Fire </li></ul>
  8. 8. But? <ul><li>I Survived </li></ul>
  9. 9. So I went Looking for Answers <ul><li>What there was, wasn't much </li></ul><ul><li>However I did learn 10 different ways to say “Hello World” </li></ul><ul><li>What there was, didn't help much </li></ul><ul><li>What there was, and still is, was dated </li></ul><ul><li>What there was, usually ended with </li></ul><ul><li>“ Read Perl Guts” </li></ul>
  10. 10. Why Is This? <ul><li>This sums up the nature of XS programming </li></ul><ul><li>There are 10 ways to do the same thing! </li></ul><ul><li>None of them are 100% wrong </li></ul><ul><li>None of them are 100% right </li></ul>
  11. 11. Lets go back to the start <ul><ul><li>We all know that Perl is written in 'C' </li></ul></ul><ul><ul><li>Big Deal most languages are written in 'C' </li></ul></ul><ul><ul><li>Most 'C' stuff will be familiar to a 'Perl' programmer </li></ul></ul><ul><ul><li>Perl is very cozy with 'C' </li></ul></ul>
  12. 12. A Few Things to know about 'C' <ul><li>Can't do shit without a 'lib' </li></ul><ul><li>“I stand on the shoulders of giants!” </li></ul><ul><li>NANOS GIGANTIUM HUMERIS INSIDENTES </li></ul><ul><ul><li>Compiled </li></ul></ul><ul><ul><li>Strictly Typed </li></ul></ul><ul><ul><li>No Memory Management </li></ul></ul><ul><ul><li>Function params are by value only </li></ul></ul>
  13. 13. A Few Things to say about Perl <ul><ul><li>Interpreted </li></ul></ul><ul><ul><li>Loosely Typed </li></ul></ul><ul><ul><li>Managed Memory </li></ul></ul><ul><ul><li>Function params are by 'Ref' only </li></ul></ul><ul><ul><li>Perl is Just 'C' without types </li></ul></ul>
  14. 14. Stop, Look, Listen! <ul><ul><li>What is it we are trying to do? </li></ul></ul><ul><ul><li>Credits to Navy Beans it is already on CPAN </li></ul></ul><ul><ul><li>Access an API </li></ul></ul><ul><ul><li>Improve performance of Perl </li></ul></ul><ul><ul><li>Link into a static Lib </li></ul></ul>
  15. 15. How about DBI and DBD::Oracle <ul><li>Access an API (OCI) for DBD::Oracle for sure </li></ul><ul><li>'C' for DBI to make it speedy for one </li></ul><ul><li>26 lines of code to do a fetch </li></ul><ul><li>Way back in the dark days there was a </li></ul><ul><li>direct link to OCI but it has died </li></ul><ul><li>Now with Dynamic Linking no need for static perl </li></ul>
  16. 16. Part the Second <ul><li>'C' and 'Perl' in Detail </li></ul><ul><li>Libs </li></ul><ul><li>Memory </li></ul><ul><li>Data </li></ul>
  17. 17. Data in 'C' <ul><li>“ Those that go down to the 'C' in Structs” </li></ul><ul><li>Defined Types (U32, oratext) </li></ul><ul><li>Arrays of types (int[], char[]) </li></ul><ul><li>Primitive types (int char etc) </li></ul><ul><li>Structs (named collection of types) </li></ul>
  18. 18. Struc <ul><li>struc say_hi_in { </li></ul><ul><li>char* english; </li></ul><ul><li>char* french; </li></ul><ul><li>int* binary; </li></ul><ul><li>} </li></ul><ul><li>Must be declared at compile time </li></ul><ul><li>It takes up a block of memory </li></ul><ul><li>So an unitialized struc takes up the same space as an initialized one </li></ul><ul><li>NOT A HASH!!! </li></ul>
  19. 19. Memory in 'C' <ul><li>“ If it is the Captain's mess? Let him clean it up!” </li></ul><ul><li>We have to do it. </li></ul><ul><li>malloc </li></ul><ul><li>free </li></ul><ul><li>'C' is dumb must know what type it is pointing to </li></ul><ul><li>Pointers and such *,& and ** </li></ul><ul><ul><li>* a pointer to something </li></ul></ul><ul><ul><li>& the value the Pointer is pointing to </li></ul></ul><ul><ul><li>** a pointer to a pointer </li></ul></ul><ul><ul><li>char * name; </li></ul></ul>
  20. 20. Libs in 'C' <ul><li>Points to an Object file .obj, .o, .so, .dll or even an exe </li></ul><ul><li>That can be shared </li></ul><ul><li>A .h or 'header file' is not necessarily code it is just a promise of code needed at compile time. </li></ul><ul><li>Usually just a prototype of the function or struct. </li></ul>
  21. 21. Data in Perl <ul><li>Metadata </li></ul><ul><li>struc sv { </li></ul><ul><li>void* sv_any; </li></ul><ul><li>U32 sv_refcnt; </li></ul><ul><li>U32 sv_flags; </li></ul><ul><li>} </li></ul><ul><li>or Perl knows about itself. </li></ul><ul><li>This is ' undef ' in Perl!!!! </li></ul>
  22. 22. Our first look of the “Guts” of Perl <ul><li>struc sv { </li></ul><ul><li>void* sv_any; </li></ul><ul><li>U32 sv_refcnt; </li></ul><ul><li>U32 sv_flags; </li></ul><ul><li>} </li></ul><ul><li>sv_any </li></ul><ul><li>Is a pointer to any other of the 'Perl' strucs </li></ul><ul><li>sv_refcnt </li></ul><ul><li>A count of the number of times this struc has been referenced </li></ul><ul><li>sv_flags </li></ul><ul><li>Holds the metadata of a 'Perl' variable. </li></ul><ul><ul><li>It utilizes bit logic (bitmask) </li></ul></ul>
  23. 23. Memory in 'PERL' II <ul><li>When an object is referenced it is </li></ul><ul><li>Incremented by one </li></ul><ul><li>sv_refcnt </li></ul><ul><ul><li>When an object goes out of scope it is </li></ul></ul><ul><ul><li>decremented by one </li></ul></ul><ul><ul><li>(most times) </li></ul></ul><ul><ul><li>(most times) </li></ul></ul><ul><ul><li>When it reaches 0 it is garbage collected by Perl </li></ul></ul><ul><ul><li>And the memory is freed </li></ul></ul>
  24. 24. Libs in 'Perl' <ul><li>A .pm file is code t hat can be shared . </li></ul>
  25. 25. How 'C' and 'Perl' Work <ul><li>'C' </li></ul><ul><ul><li>Source -> </li></ul></ul><ul><ul><li>Compiler -> </li></ul></ul><ul><ul><li>Machine Code </li></ul></ul><ul><li>'Perl' </li></ul><ul><ul><li>Sour c e -> </li></ul></ul><ul><ul><li>Interpreter -> </li></ul></ul><ul><ul><li>Syntax Tree -> </li></ul></ul><ul><ul><li>Interpreter -> </li></ul></ul><ul><ul><li>Machine Code </li></ul></ul><ul><ul><li>Syntax Tree-> </li></ul></ul><ul><ul><li>{opcodes} = </li></ul></ul><ul><ul><li>struc{} </li></ul></ul><ul><ul><li>struc{} ->xsub pointer </li></ul></ul><ul><ul><li>If this just happens to point to a 'C' function </li></ul></ul><ul><ul><li>It will run </li></ul></ul>
  26. 26. Finally down to Brass Tacks <ul><li>Thank you good by:) </li></ul><ul><ul><li>X </li></ul></ul><ul><ul><li>External </li></ul></ul><ul><ul><li>S </li></ul></ul><ul><ul><li>Subroutines </li></ul></ul><ul><ul><li>eXternal Subroutines or </li></ul></ul><ul><ul><li>XS </li></ul></ul>
  27. 27. Now Some Code <ul><li>Far be it form me to break with tradition </li></ul><ul><li>#Include <stdio.h> </li></ul><ul><li>void print_hi_larry(void) { </li></ul><ul><li>printf(“Hello Larry!”); </li></ul><ul><li>} </li></ul><ul><li>Int main(int argc,char *argv[]){ </li></ul><ul><li>print_hi_larry(); </li></ul><ul><li>exit(0); </li></ul><ul><li>} </li></ul><ul><ul><li>So to the left is hello Larry in 'C' </li></ul></ul>
  28. 28. How about a Perl File for it <ul><li>lib/Hi_ larry.pm </li></ul><ul><li>Hi_ larry.xs </li></ul><ul><li>Makefile.PL </li></ul><ul><li>README </li></ul><ul><li>t/test.t </li></ul><ul><li>Changes </li></ul><ul><li>MANIFEST </li></ul><ul><ul><li>Like most things in perl we have a Helper called </li></ul></ul><ul><ul><li>h2sx </li></ul></ul><ul><ul><li>In our case we do </li></ul></ul><ul><ul><li>H2sx -A -n Hi_ larry </li></ul></ul><ul><ul><li>And it generates all of our perl files thank you very much </li></ul></ul>
  29. 29. So all we do is open the .xs file and add in our includes and function but not the main <ul><li>#include “EXTERN.h” </li></ul><ul><li>#include perl.h” </li></ul><ul><li>#include XSUB.h” </li></ul><ul><li>#Include <stdio.h> </li></ul><ul><li>void print_hi_ larry (void) { </li></ul><ul><li>printf(“Hello Larry !”); </li></ul><ul><li>} </li></ul><ul><li>MODULE = Hi_ larry PACKAGE = Hi_ larry </li></ul><ul><li>void </li></ul><ul><li>print_hi_ larry (); </li></ul>
  30. 30. Now edit the Hi_ larry.pm <ul><li>package Hi_ larry ; </li></ul><ul><li>use 5.008009; </li></ul><ul><li>use strict; </li></ul><ul><li>use warnings; </li></ul><ul><li>Use base (qw(Exporter DynaLoader)) </li></ul><ul><li>@EXPORT_OK </li></ul><ul><li>our @EXPORT = qw( print_hi_ larry () ); </li></ul><ul><li>our $VERSION = '0.01'; </li></ul><ul><li>1; </li></ul><ul><li>__END__ </li></ul>
  31. 31. How about the test file as well <ul><li>use Test::More tests => 2; </li></ul><ul><li>BEGIN { use_ok('Hi_larry') }; </li></ul><ul><li>ok(Hi_ larry ::print_hi_ larry ,&quot;Hello Larry &quot;) </li></ul>
  32. 32. Now everyones fab <ul><li>Perl Makefile.PL </li></ul><ul><ul><li>c:>nmake test </li></ul></ul><ul><ul><li>And at the very end </li></ul></ul><ul><ul><li>t/Hi_larry....ok </li></ul></ul><ul><ul><li>Files=1, Tests=2, 0 wallclock secs ( 0.00 cusr + 0.00 csys = 0.00 CPU) </li></ul></ul><ul><ul><li>All tests successful </li></ul></ul><ul><ul><li><--John don't forget to Insert lines of compiler gobbliegook here --> </li></ul></ul>
  33. 33. So what is going on? <ul><li>Well this really comes down to black majik and it differs form OS to OS and perl to perl lets just say </li></ul><ul><li>1) our XS code is converted to 'C' (xsubpp) </li></ul><ul><li>2) this code is compiled </li></ul><ul><li>3) that compiled code is then transformed into something that can be shared into our 'Perl' </li></ul>
  34. 34. Some more Black Majik <ul><li>We did get a generated '.c' file that you can look at that at your own peril. </li></ul><ul><li>But how does xsubpp know how to get data back and forth between 'C' and 'Perl'. </li></ul><ul><li>Well we know everything in Perl is a struc so it is a rather simple trick. It uses a typemap. </li></ul>
  35. 35. Typemap <ul><ul><li>Not much to it. A simple lookup table broken into three parts </li></ul></ul><ul><ul><li>Basic types </li></ul></ul><ul><ul><li>unsigned Int T_UV </li></ul></ul><ul><ul><li>unsigned Long T_UV </li></ul></ul><ul><ul><li>size_t T_UV </li></ul></ul><ul><ul><li>STRLEN T_UV </li></ul></ul><ul><ul><li>Input types </li></ul></ul><ul><ul><li>T_UV $var =($type)SvUV($arg) </li></ul></ul><ul><ul><li>Output types </li></ul></ul><ul><ul><li>T_UV sv_setuv($arg,(UV)$var) </li></ul></ul><ul><ul><li>Hey look at that we see perl guts again </li></ul></ul>
  36. 36. <ul><ul><li>To h2xs or not h2xs </li></ul></ul><ul><ul><li>To start yes </li></ul></ul><ul><ul><li>Good Beginnings = Good Endings </li></ul></ul><ul><ul><li>Keep It Simple Stupid </li></ul></ul><ul><ul><li>Kiss </li></ul></ul><ul><ul><li>Big differences between versions and systems </li></ul></ul><ul><ul><li>What might be good for the writer of h2sx might not be good for you </li></ul></ul><ul><ul><li>Get your 'C' to compile first </li></ul></ul><ul><ul><li>For a straight Lib port Yeah OK </li></ul></ul>
  37. 37. What is in an .XS <ul><ul><li>Many ways to do anything and few rules </li></ul></ul><ul><ul><li>Chaos is great isn't it </li></ul></ul><ul><ul><li>Not quite 'Perl' and not quite 'C' </li></ul></ul><ul><ul><li>Not a Language </li></ul></ul><ul><ul><li>A large set of 'C' macros that we can use </li></ul></ul><ul><ul><li>Always in upper case </li></ul></ul><ul><ul><li>and most times followed by a “:” </li></ul></ul>
  38. 38. Anatomy of an xs file <ul><ul><li>Starts with the three wise men </li></ul></ul><ul><ul><li>#include &quot;EXTERN.h&quot; </li></ul></ul><ul><ul><li>#include &quot;perl.h&quot; </li></ul></ul><ul><ul><li>#include &quot;XSUB.h&quot; </li></ul></ul><ul><ul><li>Next our C code or Not </li></ul></ul><ul><ul><li>In DBD::ORACLE there is none we compile it first and use </li></ul></ul><ul><ul><li>#include &quot;Oracle.h&quot; </li></ul></ul><ul><ul><li>As our only line of 'C' code in oracle.xs </li></ul></ul><ul><ul><li>At any time we can do our old 'C' friend </li></ul></ul><ul><ul><li>#ifdef </li></ul></ul>
  39. 39. MODULE, PACKAGE, PREFIX, come next <ul><ul><li>MODULE = Hi_ larry PACKAGE = Hi_ larry </li></ul></ul><ul><li>MODULE </li></ul><ul><li>So anything before the first one is 'C' </li></ul><ul><li>PACKAGE </li></ul><ul><li>Says my XS code starts here </li></ul><ul><li>Lets us organize our code </li></ul><ul><li>We can switch them up so your code can be non- contiguous </li></ul>
  40. 40. MODULE = Apache PACKAGE = Apache PREFIX = mod_perl INCLUDE: Oracle.xsi We can name it anything we want as long as it is an xs file .xsi seems to be the norm PREFIX Does the opposite of what you would think Strips off the prefix from the 'XS' functions I. E. Apache.xs int mod_perl_sent_header(r, val=0) And in perl it is called as sent_header(r, val=0) Next!
  41. 41. Now your Xsub <ul><ul><li>The simplest form has three parts </li></ul></ul><ul><ul><li>A return value description </li></ul></ul><ul><ul><li>And finally a description of the argument types </li></ul></ul><ul><ul><li>The name of the Xsub and its arguments </li></ul></ul><ul><ul><li>print_hi_ larry (); </li></ul></ul><ul><ul><li>void </li></ul></ul><ul><ul><li><--John rember to change this to one that has arguments --> </li></ul></ul>
  42. 42. Some Examples <ul><li>#include <string.h> </li></ul><ul><li>STRLEN </li></ul><ul><li>strconcat(char* str1,char* str2, char* outstr){ </li></ul><ul><li>strcpy(outstr, (const char*) srt1); </li></ul><ul><li>strcat(outstr,(const char*) str2); </li></ul><ul><li>return strlen(outstr); </li></ul><ul><li>} </li></ul><ul><ul><li>And now the XS part </li></ul></ul><ul><ul><li>STRLEN </li></ul></ul><ul><ul><li>strconcat(str1, str2) </li></ul></ul><ul><ul><li>CODE: </li></ul></ul><ul><ul><li>PREINIT: </li></ul></ul><ul><ul><li>char* str1 </li></ul></ul><ul><ul><li>char* str2 </li></ul></ul><ul><ul><li>STRLEN length </li></ul></ul><ul><ul><li>length = strlen( str1) + strlen(str2)+1; </li></ul></ul><ul><ul><li>New(0,RETVAL,length,char); </li></ul></ul><ul><ul><li>OUTPUT: </li></ul></ul><ul><ul><li>RETVAL </li></ul></ul><ul><ul><li>Safefree(RETVAL); </li></ul></ul><ul><ul><li>CLEANUP: </li></ul></ul><ul><ul><li>length = strconcat(str1,str2,RETVAL); </li></ul></ul>
  43. 43. The Black Majik <ul><ul><li>PREINIT: </li></ul></ul><ul><ul><li>CODE: </li></ul></ul><ul><ul><li>OUTPUT: </li></ul></ul><ul><ul><li>RETVAL </li></ul></ul><ul><ul><li>Lets you declare extra variables </li></ul></ul><ul><ul><li>'C' code follows me </li></ul></ul><ul><ul><li>A special 'C' value just for you. That holds the 'return value' </li></ul></ul><ul><ul><li>Gives us a way to clean any messes we made </li></ul></ul><ul><ul><li>CLEANUP: </li></ul></ul><ul><ul><li>Used with CODE: to send RETVAL back to 'Perl' </li></ul></ul>
  44. 44. The Evil Dark Majik with a -6 saving throw <ul><ul><li>Safefree(RETVAL); </li></ul></ul><ul><ul><li>void New(0,void* pointer, int length, type): </li></ul></ul><ul><ul><li>Perl Guts </li></ul></ul><ul><ul><li>This allocates some memory for us. </li></ul></ul><ul><ul><li>New(0,RETVAL,length,char): </li></ul></ul><ul><ul><li>length is the new length we calculated </li></ul></ul><ul><ul><li>char is what it is </li></ul></ul><ul><ul><li>void Safefree(void* pointer); </li></ul></ul><ul><ul><li>This deallocates some memory for us. </li></ul></ul><ul><ul><li>RETVAL which is a pointer to the memory we allocated </li></ul></ul><ul><ul><li>RETVAL which is a pointer to the memory we allocated </li></ul></ul>
  45. 45. Interface Design <ul><ul><li>Make it as Perlish as possible </li></ul></ul><ul><ul><li>my $length = strconcat($str1.$str2,$outstring); </li></ul></ul><ul><ul><li>Is just plain bad. </li></ul></ul><ul><ul><li>Do not supply what we already know </li></ul></ul><ul><ul><li>my $fullname = full_name($first,$len_first,$last,$len_last) </li></ul></ul><ul><ul><li>Use PREFEX </li></ul></ul><ul><ul><li>MODULE = OCI PCKAGE = OCI </li></ul></ul><ul><ul><li>SV </li></ul></ul><ul><ul><li>oci_ping() </li></ul></ul><ul><ul><li>Would come out in perl as OCI::oci_ping() </li></ul></ul>
  46. 46. How About Summing an Array <ul><ul><li>Here is our little 'C' that takes the number of ints to add, and an array of ints </li></ul></ul><ul><ul><li>typedef int intArray; </li></ul></ul><ul><ul><li>int sum(int num, intArray *array){ </li></ul></ul><ul><ul><li>int thesum =0; </li></ul></ul><ul><ul><li>int thecount; </li></ul></ul><ul><ul><li>for (count = 0; count <num;count++){ </li></ul></ul><ul><ul><li>thesum += array[count]; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>return thesum; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  47. 47. And our XS <ul><ul><li>int </li></ul></ul><ul><ul><li>sum_a_list(array, ...) </li></ul></ul><ul><ul><li>'...' is another XS thing meaning I do not know how many arguments there are </li></ul></ul><ul><ul><li>IntArray * array </li></ul></ul><ul><ul><li>Will not compile. A XS does not know what an 'IntArray' is. </li></ul></ul><ul><ul><li>We solve this by adding our own typemap file in the build dir </li></ul></ul><ul><ul><li>intArray * T_ARRAY </li></ul></ul><ul><ul><li>CODE: </li></ul></ul><ul><ul><li>RETVAL = sum(ix_array,array); </li></ul></ul><ul><ul><li>OUTPUT: </li></ul></ul><ul><ul><li>RETVAL </li></ul></ul><ul><ul><li>CLEANUP: </li></ul></ul><ul><ul><li>Safefree(array); </li></ul></ul><ul><ul><li>XS does know what a T_ARRAY is though </li></ul></ul>
  48. 48. More Black Majik <ul><ul><li>ix_array </li></ul></ul><ul><ul><li>You might of noticed this little fellow </li></ul></ul><ul><ul><li>'...' will create the 'itmes' value for you automatically </li></ul></ul><ul><ul><li>This is the 'items' of the '...' that was passed in on the stack. </li></ul></ul><ul><ul><li>Int </li></ul></ul><ul><ul><li>not_number_count(...) </li></ul></ul><ul><ul><li>CODE: </li></ul></ul><ul><ul><li>for(i=0;i< itmes; i++){ </li></ul></ul>
  49. 49. The Stack <ul><ul><li>We have our one Gut Macro just for called ST(i) where i is the 0 based index of the stack </li></ul></ul><ul><ul><li>We might as well talk about the 'stack' </li></ul></ul><ul><ul><li>So in our last example if we add </li></ul></ul><ul><ul><li>SV *sv = ST(i); </li></ul></ul><ul><ul><li>As the next line. We would be saving a referace to the Stack value in a local SV called sv. </li></ul></ul><ul><ul><li>One thing with the Stack is it normally have only one value on the way back (RETVAL) </li></ul></ul><ul><ul><li>Until now! </li></ul></ul>
  50. 50. Just Plain Eval Majik <ul><ul><li>Many times in Perl we want to return a list of alternating keys and values </li></ul></ul><ul><ul><li>Here is some of the XS code for this. </li></ul></ul><ul><ul><li>%say_hi = Hello::say_hi_in('eu'); </li></ul></ul><ul><ul><li>Print $say_hi{'french'}; </li></ul></ul><ul><ul><li>void </li></ul></ul><ul><ul><li>say_hi_in(local) </li></ul></ul><ul><ul><li>char * local </li></ul></ul><ul><ul><li>PPCODE: </li></ul></ul><ul><ul><li>switch (local) { </li></ul></ul><ul><ul><li>case REGION_EU: </li></ul></ul><ul><ul><li>EXTEND(SP,16); </li></ul></ul><ul><ul><li>PUSHs(sv_2mortal(newSVpv(“french”,”Bonjour”))); </li></ul></ul><ul><ul><li>PUSHs(sv_2mortal(newSVpv(“english”,”Hello”))); </li></ul></ul>
  51. 51. The explanation <ul><ul><li>PPCODE: </li></ul></ul><ul><ul><li>Tells XS that I will be creating the return Stack </li></ul></ul><ul><ul><li>EXTEND(SP,16); </li></ul></ul><ul><ul><li>Tells XS to extend the return stack to 16 spots </li></ul></ul><ul><ul><li>PUSHs(sv_2mortal(newSVpv(“french”,”Bonjour”))); </li></ul></ul><ul><ul><li>PUSHs puts our value on the Stack </li></ul></ul><ul><ul><li>sv_2mortal </li></ul></ul><ul><ul><li>More Guts Majik. In this case we have to tell 'Perl' that the new value we are pushing is Mortal as only copy of the sv is assigned in Perl. So now they will be garbage collected. </li></ul></ul><ul><ul><li>newSVpv </li></ul></ul><ul><ul><li>Another Guts macro that creates a SV from a string. </li></ul></ul>
  52. 52. How about embedding PERL <ul><ul><li>Surprisingly easy </li></ul></ul><ul><ul><li>#includes <EXTERN.h> </li></ul></ul><ul><ul><li>#includes <perl.h> </li></ul></ul><ul><ul><li>You Just include it. </li></ul></ul><ul><ul><li>static PerlInterpreter *my_perl; </li></ul></ul><ul><ul><li>int main(intargc,char **argv){ </li></ul></ul><ul><ul><li>Char* command_line[] ={“”.”-e”,”print ”Hello C this is Larry ”;”}; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>my_perl = perl_alloc(); </li></ul></ul><ul><ul><li>perl_constrct(my_perl); </li></ul></ul><ul><ul><li>perl_run(my_perl); </li></ul></ul><ul><ul><li>perl_parse(my_perl, NULL,3, command_line,(char **)NULL); </li></ul></ul><ul><ul><li>perl_destruct(my_perl); </li></ul></ul><ul><ul><li>perl_free(my_perl); </li></ul></ul><ul><ul><li>return 0; </li></ul></ul>
  53. 53. Where to go from here <ul><ul><li>Well there is only 1 book </li></ul></ul><ul><ul><li>Extending and Embedding Perl </li></ul></ul><ul><ul><li>By Tim Jeness and Simon Cozens </li></ul></ul><ul><ul><li>Steal from other people like I do </li></ul></ul>
  54. 54. ? <ul><li>Its not the Falling the hurts </li></ul><ul><li>It is the sudden stop at the bottom that will kill you </li></ul>