1 / 93

Pathologically Polluting Perl (with C, Java and Other Rubbish using Inline.pm)

Pathologically Polluting Perl (with C, Java and Other Rubbish using Inline.pm). Brian Ingerson ActiveState Thursday June 14th, 19101 Leacock 132 Yet Another Perl Conference. Inline.pm. Pathologically Polluting Perl (with C, Python and Other Rubbish using Inline.pm). Brian Ingerson

didier
Download Presentation

Pathologically Polluting Perl (with C, Java and Other Rubbish using Inline.pm)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pathologically Polluting Perl(with C, Java and Other Rubbish using Inline.pm) Brian Ingerson ActiveState Thursday June 14th, 19101 Leacock 132 Yet Another Perl Conference Inline.pm

  2. Pathologically Polluting Perl(with C, Python and Other Rubbish using Inline.pm) Brian Ingerson ActiveState Wednesday July 25th 3:45pm Grande Ballroom C The Perl Conference 5 Inline.pm

  3. Part I Introducing Inline.pm C Perl Run Whence Inline? One-Liners The Inline Syntax Inline::C Supported Platforms Inline.pm

  4. Introducing Inline.pm Inline lets you write Perl subroutines ... ... in other programming languages! Functions, methods and classes too. Put some other code directly into your script, and simply run it like normal. It just works. Inline takes care of all the details! Inline.pm

  5. C Perl Run Hello, world Hello, foo A 'Length'y Discussion Inline.pm

  6. hello_world.pl use Inline C => <<'END_C'; #include <stdio.h> void greet() { printf("Hello, world\n"); } END_C greet; Inline.pm

  7. hello_world.pl use Inline C => <<'END_C'; #include <stdio.h> void greet() { printf("Hello, world\n"); } END_C greet; Inline.pm

  8. hello_world.pl use Inline C => <<'END_C'; #include <stdio.h> void greet() { printf("Hello, world\n"); } END_C greet; Inline.pm

  9. AUTO_INCLUDE #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "INLINE.h" Inline.pm

  10. hello_foo.pl use Inline C; greet('foo'); __END__ __C__ void greet(char* who) { printf("Hello, %s\n", who); } Inline.pm

  11. hello_foo.pl use Inline C; greet('foo'); __END__ __C__ void greet(char* who) { printf("Hello, %s\n", who); } Inline.pm

  12. hello_foo.pl use Inline C; greet('foo'); __END__ __C__ void greet(char*who) { printf("Hello, %s\n", who); } Inline.pm

  13. length.pl use Inline C; open DICT, '/usr/dict/words' or die $!; undef $/; print "The length of /usr/dict/words is ", l(<DICT>), " characters\n"; __END__ __C__ int l(char* str) { return strlen(str); } Inline.pm

  14. length.pl use Inline C; open DICT, '/usr/dict/words' or die $!; undef $/; print "The length of /usr/dict/words is ", len(<DICT>), " characters\n"; __END__ __C__ intlen(char*str) { return strlen(str); } Inline.pm

  15. Whence Inline? Inline came forth from Altered States: Inline.pm • TPC • DDC • PRD • MDV • The Perl Conf 4 • Dr. Damian Conway • Parse::RecDescent • Digest MD5 • Happy Birthday!

  16. One-liners Print all primes perl -le 'while(($_||=1)++){print if(1x$_)!~/^(11+)\1+$/}' Crash your Pentium perl -e 'require DynaLoader;DynaLoader::dl_install_xsub("main::hangme",unpack("I", pack("P4", "\xF0\x0F\xC7\xC8")));hangme()' A scary JAPH perl -e 'BEGIN{my$x="Knuth heals rare project\n";$^H {integer}=sub{my$y=shift;$_=substr$x=>$y&0x1F,1;$y>32?uc:lc};$^H=hex join""=>2,1,1,0,0}print 52,2,10,23,16,8,1,19,3,6,15, 12,5,49,21,14,9,11,36,13,22,32,7,18,24;' Inline.pm

  17. Inline One-liners JAPH perl -e 'use Inline C=>q{void J(){printf("Just Another Perl Hacker\n");}};J' JAxH perl -le 'use Inline C=>q{SV*JAxH(char*x){return newSVpvf("Just Another %s Hacker",x);}};print JAxH+Perl' The Muenzerbrot Set use Inline C=>'void C(){int m,u,e=0;float l,_,I;for(;1840-e;putchar((++e>907 &&942>e?61-m:u)["\n)moc.isc@rezneumb(rezneuM drahnreB"]))for(u=_=l=0;79-(m =e%80)&&I*l+_*_<6&&26-++u;_=2*l*_+e/80*.09-1,l=I)I=l*l-_*_-2+m/27.;}';&C Inline.pm

  18. The Inline Syntax Basic Syntax use Inline C => "source code"; "String" use Inline C => <<'END_C'; 'DATA' use Inline C => 'DATA'; 'FILE' use Inline::Files; use Inline C => 'FILE'; Inline.pm

  19. The Inline Syntax Basic Configuration use Inline C => "source code", LIBS => '-lfoo', PREFIX => 'foo_'; General Config Options NAME, DIRECTORY Language ('C') Config Options LIBS, INC, AUTO_INCLUDE Inline.pm

  20. The Inline Syntax Shorthand use Inline C => 'DATA'; use Inline C => 'FILE'; use Inline C; Inline.pm

  21. The Inline Syntax Inline.pm • Inline has two functions: bind() and init() • Both functions are rarely used • bind() is like eval() for C code • Takes same args as 'use Inline ...' Inline->bind(C => $code); • init() is the runtime equiv of INIT() Inline->init;

  22. Inline::C Basic Perl5 Internals Hacking Internals Wrapping External Libraries The Perl Stack Inline.pm

  23. Basic Perl5 Internals Inline.pm SV* The Perl STACK Typemaps perlapi.pod

  24. Scalar Values (SV*) Inline.pm http://gisle.aas.no/perl/illguts/

  25. Perl5 API (perlapi.pod) Inline.pm SV* newSVpvf(const char* pat, ...) void sv_catpvf(SV* sv, const char* pat, ...) void SvGROW(SV* sv, STRLEN len) SV* newSViv(IV i) SV* sv_2mortal(SV* sv) char* SvPV(SV* sv, STRLEN len) char* SvPV_nolen(SV* sv) U32 SvREFCNT(SV* sv) bool SvROK(SV* sv) bool SvIOK(SV* sv) STRLEN SvLEN(SV* sv) SV* get_sv(const char* name, I32 create) I32 call_pv(const char* sub_name, I32 flags) SV* eval_pv(const char* p, I32 croak_on_err) void croak(const char* pat, ...)

  26. Hacking Internals Inline.pm Inline makes it easy to mess around with Perl internals Caveat Scriptor! READONLY Lost and Found

  27. writable.pl Inline.pm use Inline C; $str = \ "Ingy hates Inline\n"; writable($$str); $$str =~ s/hate/love/g; print $$str; __END__ __C__ void writable(SV* x) {SvREADONLY_off(x);}

  28. writable.pl Inline.pm use Inline C; $str = \ "Ingy hates Inline\n"; writable($$str); $$str =~ s/hate/love/g; print $$str; __END__ __C__ void writable(SV* x) {SvREADONLY_off(x);}

  29. lostfound.pl Inline.pm use Inline C; $x = q{"Everybody Loves My Ingy"}; $x = 42; find_string($x); print "My favorite number is $x\n"; __END__ __C__ void find_string(SV* x) { SvPOK_on(x); }

  30. lostfound.pl Inline.pm use Inline C; $x = q{"Everybody Loves My Ingy"}; $x = 42; find_string($x); print "My favorite number is $x\n"; __END__ __C__ void find_string(SV* x) { SvPOK_on(x); }

  31. Wrapping External Libraries Inline.pm LIBS INC PREFIX TYPEMAPS AUTOWRAP

  32. WinJAPH.pl use Inline C => DATA => LIBS => '-luser32', PREFIX => 'my_'; MessageBoxA('Inline Message Box', 'Just Another Perl Hacker'); __END__ __C__ #include <windows.h> int my_MessageBoxA(char* Caption, char* Text) { return MessageBoxA(0, Text, Caption, 0); } Inline.pm

  33. WinJAPH.pl use Inline C => DATA => LIBS => '-luser32', PREFIX => 'my_'; MessageBoxA('Inline Message Box', 'Just Another Perl Hacker'); __END__ __C__ #include <windows.h> int my_MessageBoxA(char* Caption, char* Text) { return MessageBoxA(0, Text, Caption, 0); } Inline.pm

  34. localtime.pl print map {"$_\n"} get_localtime(time); use Inline C => <<'END_OF_C_CODE'; #include <time.h> void get_localtime(int utc) { struct tm *ltime = localtime(&utc); Inline_Stack_Vars; Inline_Stack_Reset; Inline_Stack_Push(newSViv(ltime->tm_year)); Inline_Stack_Push(newSViv(ltime->tm_mon)); Inline_Stack_Push(newSViv(ltime->tm_mday)); Inline_Stack_Push(newSViv(ltime->tm_hour)); Inline_Stack_Push(newSViv(ltime->tm_min)); Inline_Stack_Push(newSViv(ltime->tm_sec)); Inline_Stack_Push(newSViv(ltime->tm_isdst)); Inline_Stack_Done; } END_OF_C_CODE Inline.pm

  35. localtime.pl print map {"$_\n"} get_localtime(time); use Inline C => <<'END_OF_C_CODE'; #include <time.h> void get_localtime(int utc) { struct tm *ltime = localtime(&utc); Inline_Stack_Vars; Inline_Stack_Reset; Inline_Stack_Push(newSViv(ltime->tm_year)); Inline_Stack_Push(newSViv(ltime->tm_mon)); Inline_Stack_Push(newSViv(ltime->tm_mday)); Inline_Stack_Push(newSViv(ltime->tm_hour)); Inline_Stack_Push(newSViv(ltime->tm_min)); Inline_Stack_Push(newSViv(ltime->tm_sec)); Inline_Stack_Push(newSViv(ltime->tm_isdst)); Inline_Stack_Done; } END_OF_C_CODE Inline.pm

  36. Supported Platforms for C All Modern Unix Variants All Modern Windows & Cygwin Requires Perl's Build Environment Binary Distribution Support Works with Mingw32/gcc on Win32 Working on VMS et al. Supports Perl 5.005_03 and up. Inline.pm

  37. Part II Object Oriented C Inline C++ How Inline Works Inline::Python Inline::Java Inline::ASM Inline.pm

  38. Object Oriented C Perl can use C based objects The OO behaviour is handled by Perl The data is on accessible from C Inline.pm

  39. oopc.pl my $obj1 = Soldier->new('Benjamin', 'Private', 11111); my $obj2 = Soldier->new('Sanders', 'Colonel', 22222); my $obj3 = Soldier->new('Matt', 'Sergeant', 33333); for my $obj ($obj1, $obj2, $obj3) { print ($obj->get_serial, ") ", $obj->get_name, " is a ", $obj->get_rank, "\n"); } Inline.pm

  40. oopc.pl package Soldier; use Inline C => <<'END'; typedef struct { char* name; char* rank; long serial; } Soldier; Inline.pm

  41. oopc.pl Inline.pm SV* new(char* class, char* name, char* rank, long serial) { Soldier* soldier = malloc(sizeof(Soldier)); SV* obj_ref = newSViv(0); SV* obj = newSVrv(obj_ref, class); soldier->name = strdup(name); soldier->rank = strdup(rank); soldier->serial = serial; sv_setiv(obj, (IV)soldier); SvREADONLY_on(obj); return obj_ref; }

  42. oopc.pl char* get_name(SV* obj) { return ((Soldier*)SvIV(SvRV(obj)))->name; } char* get_rank(SV* obj) { return ((Soldier*)SvIV(SvRV(obj)))->rank; } long get_serial(SV* obj) { return ((Soldier*)SvIV(SvRV(obj)))->serial; } Inline.pm

  43. oopc.pl void DESTROY(SV* obj) { Soldier* soldier = (Soldier*)SvIV(SvRV(obj)); free(soldier->name); free(soldier->rank); free(soldier); } Inline.pm

  44. Some Ware Beyond the C The primary goal of Inline is: "Make it easy to use other programming languages with Perl" This is not limited to C. Drumroll please… Inline.pm

  45. Enter Neil Watkiss! Inline.pm

  46. Inline::CPP Inline.pm use Inline 'C++'; my $obj1 = Soldier->new('Benjamin', 'Private', 11111); __END__ __C++__ classSoldier { public: Soldier(char *name, char *rank, int serial); char *get_name(); char *get_rank(); int get_serial(); private: char *name; char *rank; int serial; };

  47. How Inline Works Inline's implementation is simple. It makes use of existing Perl tools. It just does a *lot* of little things. It's magic. Inline.pm

  48. The Friends of Inline Parse::RecDescent Digest::MD5 Config.pm XS ExtUtils::MakeMaker DynaLoader perl ! Inline.pm

  49. The C Parse::RecDescent Grammar Inline.pm c_code: part(s) {1} part: comment | function_definition { my $function = $item[1]->[0]; push @{$thisparser->{data}->{functions}}, $function; -- lines deleted -- } | anything_else comment: m{\s* // [^\n]* \n }x | m{\s* /\* (?:[^*]+|\*(?!/))* \*/ ([ \t]*)? }x function_definition: -- lines deleted -- anything_else: /.*/

More Related