Inline pm Pathologically Polluting Perl with C Java
- Slides: 93
Inline. pm Pathologically Polluting Perl (with C, Java and Other Rubbish using Inline. pm) Brian Ingerson Active. State Thursday June 14 th, 19101 Leacock 132 Yet Another Perl Conference
Inline. pm Pathologically Polluting Perl (with C, Python and Other Rubbish using Inline. pm) Brian Ingerson Active. State Wednesday July 25 th 3: 45 pm Grande Ballroom C The Perl Conference 5
Inline. pm Part I • • Introducing Inline. pm C Perl Run Whence Inline? One-Liners The Inline Syntax Inline: : C Supported Platforms
Inline. pm 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 C Perl Run • Hello, world • Hello, foo • A 'Length'y Discussion
Inline. pm hello_world. pl use Inline C => <<'END_C'; #include <stdio. h> void greet() { printf("Hello, worldn"); } END_C greet;
Inline. pm hello_world. pl use Inline C => <<'END_C'; #include <stdio. h> void greet() { printf("Hello, worldn"); } END_C greet;
Inline. pm hello_world. pl use Inline C => <<'END_C'; #include <stdio. h> void greet() { printf("Hello, worldn"); } END_C greet;
Inline. pm AUTO_INCLUDE #include "EXTERN. h" "perl. h" "XSUB. h" "INLINE. h"
Inline. pm hello_foo. pl use Inline C; greet('foo'); __END__ __C__ void greet(char* who) { printf("Hello, %sn", who); }
Inline. pm hello_foo. pl use Inline C; greet('foo'); __END__ __C__ void greet(char* who) { printf("Hello, %sn", who); }
Inline. pm hello_foo. pl use Inline C; greet('foo'); __END__ __C__ void greet(char* who) { printf("Hello, %sn", who); }
Inline. pm length. pl use Inline C; open DICT, '/usr/dict/words' or die $!; undef $/; print "The length of /usr/dict/words is ", l(<DICT>), " charactersn"; __END__ __C__ int l(char* str) { return strlen(str); }
Inline. pm length. pl use Inline C; open DICT, '/usr/dict/words' or die $!; undef $/; print "The length of /usr/dict/words is ", len(<DICT>), " charactersn"; __END__ __C__ int len(char* str) { return strlen(str); }
Inline. pm Whence Inline? • • • Inline came forth from Altered States: TPC • The Perl Conf 4 DDC • Dr. Damian Conway PRD • Parse: : Rec. Descent MDV • Digest MD 5 Happy Birthday!
Inline. pm One-liners • Print all primes • perl -le 'while(($_||=1)++){print if(1 x$_)!~/^(11+)1+$/}' • Crash your Pentium • perl -e 'require Dyna. Loader; Dyna. Loader: : dl_install_xsub("main: : hangme", unpack( "I", pack("P 4", "x. F 0x 0 Fx. C 7x. C 8"))); hangme()' • A scary JAPH • perl -e 'BEGIN{my$x="Knuth heals rare projectn"; $^H {integer}=sub{my$y=shift; $_=substr$x=>$y&0 x 1 F, 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 Inline One-liners • JAPH • perl -e 'use Inline C=>q{void J(){printf("Just Another Perl Hackern"); }}; J' • JAx. H • perl -le 'use Inline C=>q{SV*JAx. H(char*x){return new. SVpvf("Just Another %s Hacker", x); }}; print JAx. H+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(rezneu. M drahnre. B"]))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
Inline. pm 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 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 The Inline Syntax • Shorthand use Inline C => 'DATA'; use Inline C => 'FILE'; use Inline C;
Inline. pm The Inline Syntax • • 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;
Inline. pm Inline: : C • • Basic Perl 5 Internals Hacking Internals Wrapping External Libraries The Perl Stack
Inline. pm Basic Perl 5 Internals • • SV* The Perl STACK Typemaps perlapi. pod
Inline. pm Scalar Values (SV*) • http: //gisle. aas. no/perl/illguts/
Inline. pm Perl 5 API (perlapi. pod) SV* void SV* char* U 32 bool STRLEN SV* I 32 SV* void new. SVpvf(const char* pat, . . . ) sv_catpvf(SV* sv, const char* pat, . . . ) Sv. GROW(SV* sv, STRLEN len) new. SViv(IV i) sv_2 mortal(SV* sv) Sv. PV(SV* sv, STRLEN len) Sv. PV_nolen(SV* sv) Sv. REFCNT(SV* sv) Sv. ROK(SV* sv) Sv. IOK(SV* sv) Sv. LEN(SV* sv) get_sv(const char* name, I 32 create) call_pv(const char* sub_name, I 32 flags) eval_pv(const char* p, I 32 croak_on_err) croak(const char* pat, . . . )
Inline. pm Hacking Internals • • Inline makes it easy to mess around with Perl internals Caveat Scriptor! READONLY Lost and Found
Inline. pm writable. pl use Inline C; $str = "Ingy hates Inlinen"; writable($$str); $$str =~ s/hate/love/g; print $$str; __END__ __C__ void writable(SV* x) {Sv. READONLY_off(x); }
Inline. pm writable. pl use Inline C; $str = "Ingy hates Inlinen"; writable($$str); $$str =~ s/hate/love/g; print $$str; __END__ __C__ void writable(SV* x) {Sv. READONLY_off(x); }
Inline. pm lostfound. pl use Inline C; $x = q{"Everybody Loves My Ingy"}; $x = 42; find_string($x); print "My favorite number is $xn"; __END__ __C__ void find_string(SV* x) { Sv. POK_on(x); }
Inline. pm lostfound. pl use Inline C; $x = q{"Everybody Loves My Ingy"}; $x = 42; find_string($x); print "My favorite number is $xn"; __END__ __C__ void find_string(SV* x) { Sv. POK_on(x); }
Inline. pm Wrapping External Libraries • • • LIBS INC PREFIX TYPEMAPS AUTOWRAP
Inline. pm Win. JAPH. pl use Inline C => DATA => LIBS => '-luser 32', PREFIX => 'my_'; Message. Box. A('Inline Message Box', 'Just Another Perl Hacker'); __END__ __C__ #include <windows. h> int my_Message. Box. A(char* Caption, char* Text) { return Message. Box. A(0, Text, Caption, 0); }
Inline. pm Win. JAPH. pl use Inline C => DATA => LIBS => '-luser 32', PREFIX => 'my_'; Message. Box. A('Inline Message Box', 'Just Another Perl Hacker'); __END__ __C__ #include <windows. h> int my_Message. Box. A(char* Caption, char* Text) { return Message. Box. A(0, Text, Caption, 0); }
Inline. pm 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(new. SViv(ltime->tm_year)); Inline_Stack_Push(new. SViv(ltime->tm_mon)); Inline_Stack_Push(new. SViv(ltime->tm_mday)); Inline_Stack_Push(new. SViv(ltime->tm_hour)); Inline_Stack_Push(new. SViv(ltime->tm_min)); Inline_Stack_Push(new. SViv(ltime->tm_sec)); Inline_Stack_Push(new. SViv(ltime->tm_isdst)); Inline_Stack_Done; } END_OF_C_CODE
Inline. pm 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(new. SViv(ltime->tm_year)); Inline_Stack_Push(new. SViv(ltime->tm_mon)); Inline_Stack_Push(new. SViv(ltime->tm_mday)); Inline_Stack_Push(new. SViv(ltime->tm_hour)); Inline_Stack_Push(new. SViv(ltime->tm_min)); Inline_Stack_Push(new. SViv(ltime->tm_sec)); Inline_Stack_Push(new. SViv(ltime->tm_isdst)); Inline_Stack_Done; } END_OF_C_CODE
Inline. pm Supported Platforms for C • • All Modern Unix Variants All Modern Windows & Cygwin Requires Perl's Build Environment Binary Distribution Support Works with Mingw 32/gcc on Win 32 Working on VMS et al. Supports Perl 5. 005_03 and up.
Inline. pm Part II • • • Object Oriented C Inline C++ How Inline Works Inline: : Python Inline: : Java Inline: : ASM
Inline. pm 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 oopc. pl my $obj 1 = Soldier->new('Benjamin', 'Private', 11111); my $obj 2 = Soldier->new('Sanders', 'Colonel', 22222); my $obj 3 = Soldier->new('Matt', 'Sergeant', 33333); for my $obj ($obj 1, $obj 2, $obj 3) { print ($obj->get_serial, ") ", $obj->get_name, " is a ", $obj->get_rank, "n"); }
Inline. pm oopc. pl package Soldier; use Inline C => <<'END'; typedef struct { char* name; char* rank; long serial; } Soldier;
Inline. pm oopc. pl SV* new(char* class, char* name, char* rank, long serial) { Soldier* soldier = malloc(sizeof(Soldier)); SV* obj_ref = new. SViv(0); SV* obj = new. SVrv(obj_ref, class); soldier->name = strdup(name); soldier->rank = strdup(rank); soldier->serial = serial; } sv_setiv(obj, (IV)soldier); Sv. READONLY_on(obj); return obj_ref;
Inline. pm oopc. pl char* get_name(SV* obj) { return ((Soldier*)Sv. IV(Sv. RV(obj)))->name; } char* get_rank(SV* obj) { return ((Soldier*)Sv. IV(Sv. RV(obj)))->rank; } long get_serial(SV* obj) { return ((Soldier*)Sv. IV(Sv. RV(obj)))->serial; }
Inline. pm oopc. pl void DESTROY(SV* obj) { Soldier* soldier = (Soldier*)Sv. IV(Sv. RV(obj)); free(soldier->name); free(soldier->rank); free(soldier); }
Inline. pm 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 Enter Neil Watkiss!
Inline. pm Inline: : CPP use Inline 'C++'; my $obj 1 = Soldier->new('Benjamin', 'Private', 11111); __END__ __C++__ class Soldier { public: Soldier(char *name, char *rank, int serial); char *get_name(); char *get_rank(); int get_serial(); private: char *name; char *rank; int serial; };
Inline. pm 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 The Friends of Inline • • Parse: : Rec. Descent Digest: : MD 5 Config. pm XS Ext. Utils: : Maker Dyna. Loader perl !
Inline. pm The C Parse: : Rec. Descent Grammar c_code: part: part(s) {1} 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: /. */
Inline. pm How Inline C Works 1. 2. 3. 4. Name, NAME, AUTONAME Dust for Fingerprints (. inl) Directory Assistance Build 1. 2. 3. 4. Parse Glue Compile Cache 5. Load (and Bind)
Inline. pm How Inline Foo Works 1. 2. 3. 4. Name, NAME, AUTONAME Dust for Fingerprints (. inl) Directory Assistance Build 1. 2. 3. 4. Parse Glue Compile Cache 5. Load (and Bind)
Inline. pm The 'config' file version : 0. 40 languages : % C : C C++ : CPP types : % C : compiled CPP : compiled modules : % C : Inline: : C CPP : Inline: : CPP suffixes : % C : so CPP : so
Inline. pm The '. inl' file md 5 : 2 cdb 20 c 55 cc 56102 fd 68 db 27620832 ae name : japh language : C language_id : C date_compiled : Fri Jun 1 01: 15: 55 2001 inline_version : 0. 40 ILSM : % module : Inline: : C suffix : so type : compiled Config : % apiversion : 5. 6. 1 archname : i 686 -linux-thread-multi cc : gcc osname : linux osvers : 2. 2. 17 so : so version : 5. 6. 1
Inline. pm Inline: : Python
Inline. pm snake. pl use Inline Python; my $language = shift; print $language, (match($language, 'Perl') ? ' rules' : ' sucks'), "!n"; __END__ __Python__ import sys import re def match(str, regex): f = re. compile(regex); if f. match(str): return 1 return 0
Inline. pm Inline: : Java • Meet Patrick Le. Boutillier
Inline. pm squarer. java public class squarer { public squarer() {} public double square(double x) { return x * x ; } }
Inline. pm squarer. pl use strict; use Inline Java => 'DATA', CLASSPATH => '/usr/local/j? ? '; my $s = new my_squarer(); my $n = 6; print "$n squared is ", $s->square($n), "n"; __END__ __Java__ class my_squarer extends squarer { public my_squarer() { super(); } }
Inline. pm yasquarer. pl use strict; use Inline Java => 'STUDY', CLASSPATH =>'/usr/local/j? ? ', STUDY => ['squarer']; my $s = new squarer(); my $n = 7; print "$n squared is ", $s->square($n), "n" ;
Inline. pm Inline: : ASM • Inline Assembly Language • Seriously
Inline. pm asm_japh. pl use Inline ASM => DATA => PROTOTYPES => {JAx. H => 'void(char*)'}; print JAx. H('Perl'); __END__ __C__ BITS 32 GLOBAL JAx. H EXTERN printf SECTION. text JAx. H push ebp mov ebp, esp mov eax, [ebp+8] push dword eax push dword jaxhstr call printf mov esp, ebp pop ebp ret SECTION. data jaxhstr db "Just Another %s Hacker", 10, 0
Inline. pm Part III • • The Inline API Inline: : Foo Inline: : PERL Inline: : Perl Inline: : Maker Inline: : CPR Future Plans
Inline. pm The Inline API • • • Rolling Your Own Almost any language can be Inlined 5 degrees of separation Actually 5 methods Inline-API. pod
Inline. pm The Inline API • • ILSMs are subclasses of Inline Named Inline: : Foo register() validate() build() load() info()
Inline. pm Inline: : Foo • It really exists • I use it to test Inline • You're soaking in it
Inline. pm t/01 usages. t use Test; plan( tests => 1); # test 5 # Make sure language name aliases work ('foo' instead of 'Foo') ok(test 5('test 5')); use Inline foo => <<'END_OF_FOO'; foo-sub test 5 { foo-return $_[0] foo-eq 'test 5'; } END_OF_FOO
Inline. pm Inline/Foo. pm package Inline: : Foo; $VERSION = '0. 01'; require Inline; @ISA = qw(Inline); use strict; use Carp;
Inline. pm Inline/Foo. pm sub register { return { language => 'Foo', aliases => ['foo'], type => 'interpreted', suffix => 'foo', }; }
Inline. pm Inline/Foo. pm sub validate { my $o = shift; $o->{ILSM}{PATTERN} ||= 'foo-'; $o->{ILSM}{BAR} ||= 0; while (@_) { my ($key, $value) = splice @_, 0, 2; if ($key eq 'PATTERN') { $o->{ILSM}{PATTERN} = $value; next; } if ($key eq 'BAR') { croak usage_config_bar unless $value =~ /^[01]$/; $o->{ILSM}{BAR} = $value; next; } croak usage_config($key); } }
Inline. pm Inline/Foo. pm sub build { my $o = shift; my $code = $o->{API}{code}; my $pattern = $o->{ILSM}{PATTERN}; $code =~ s/$pattern//g; $code =~ s/bar-//g if $o->{ILSM}{BAR}; sleep 1; # imitate compile delay { package Foo: : Tester; eval $code; } croak "Foo build failed: n$@" if $@; my $path = "$o->{API}{install_lib}/auto/". "$o->{API}{modpname}"; my $obj = $o->{API}{location}; $o->mkpath($path) unless -d $path; open FOO_OBJ, "> $obj" or croak "Can't open $obj for outputn$!"; print FOO_OBJ $code; close *FOO_OBJ; }
Inline. pm Inline/Foo. pm sub load { my $o = shift; my $obj = $o->{API}{location}; open FOO_OBJ, "< $obj" or croak "Can't open $obj for outputn$!"; my $code = join '', <FOO_OBJ>; close *FOO_OBJ; eval "package $o->{API}{pkg}; n$code"; croak "Unable to load Foo module $obj: n$@" if $@; }
Inline. pm Inline: : PERL • Bring the power of PERL programming, • to your Perl programs! • John Mc. Namara • Posted on perlmonks. net • Available as Acme-Inline-Perl • It's on its way to CPAN. . .
Inline. pm Inline/PERL. pm sub register { return { language => 'PERL', aliases => ['PEARL', 'CGI'], type => 'interpreted', suffix => 'PL', }; } sub build { my $o = shift; my $code = $o->{API}{code}; if ($code =~ /strict/) { carp <<END; Your code contains "use strict; ". Please be aware that this may provide you with too much of a clue. '; END
Inline. pm Inline: : Perl • Bind another Perl to Perl • Legacy Perls • Like Perl 4 • or Perl 5. 8 • or Perl 5+i
Inline. pm Inline CPAN Modules • Write an Inline Module • Add a NAME and VERSION • use Inline: : Maker; • make dist • Equivalent to XS
Inline. pm Perl. API. pm package Perl. API; use strict; require Exporter; @Perl. API: : ISA = qw(Exporter); @Perl. API: : EXPORT = qw(Sv. READONLY_off); $Perl. API: : VERSION = '0. 42'; use Inline C => DATA => PREFIX => 'my_', NAME => 'Perl. API', VERSION => '0. 42'; 1; __DATA__ __C__ void my_Sv. READONLY_off(SV* x) {Sv. READONLY_off(x); }
Inline. pm Makefile. PL use Inline: : Maker; Write. Inline. Makefile( NAME => 'Perl. API', VERSION_FROM => 'Perl. API. pm', );
Inline. pm test. pl use Test; use Perl. API; plan(tests => 1); $str = "Ingy hates Inlinen"; Sv. READONLY_off($$str); $$str =~ s/hate/love/g; ok($$str =~ /loves/);
Inline. pm See Perl Run Perl, Run!
Inline. pm Soooo…. • Inline is a great way to extend Perl w/ C • But. . . • How can we easily embed Perl in C?
Inline. pm What if…. • • We could pass plain C to Perl, And Perl could pass it to Inline, And Inline could bind to main(), And just invoke main() from Perl.
Inline. pm Introducing CPR • • • "C Perl Run" The C interpreter A brand new programming language That combines all of C With the guts of Perl #!usr/bin/cpr
Inline. pm Hello_World. cpr #!/usr/bin/cpr int main(void) { printf("Hello, worldn"); }
Inline. pm Hello_Perl. cpr #!/usr/local/bin/cpr int main(void) { printf("Hello World, I'm running under Perl version %sn", CPR_eval("use Config; $Config{version}") ); return 0; }
Inline. pm Hello_Cruel_World. cpr #!/usr/bin/cpr int main(void) { CPR_eval("use Inline (C => q{ char* greet() { return "Hello world"; } })"); printf("%s, I'm running under Perl version %sn", CPR_eval("&greet"), CPR_eval("use Config; $Config{version}")); return 0; }
Inline. pm Future Plans • • • A new frontier for Perl More ILSMs Inline 0. 50 Development Tools You!
Inline. pm Planned ILSMs • • Guile Scheme Fortran Lisp Ruby R Bash Perl
Inline. pm Inline 0. 50 Plans • • • XS like modules Viral Inline stub No core requirement Support all versions equally Encourage hacking and collaboration
Inline. pm Development Tools • The Inline Debugger • Module authoring automation • Binary distributions
Inline. pm Inline Needs YOU! • inline@perl. org • http: //inline. perl. org
Inline. pm Questions
- Double bastion inline
- What is inline function
- Inline function matlab
- Inline coating
- Cython inline
- Inline function in c++
- Inline hockey vs ice hockey
- Inline function in c++
- Single bastion inline
- What is inline function
- What is inline function
- Dreamweaver inline css
- Pdip vs dip
- Inline subquery
- Inline pulsation dampener
- Inline function matlab
- Inline hockey vs ice hockey
- Raman spectroscopy kaiser
- Cascade inline unit
- Inline internal and external javascript
- Double bastion inline
- Ich spiele inline-hockey
- Inline structure
- Css inline vs external
- Numpy.polynomial.polynomial
- Data hiding in c++
- Inline definition
- Beginning perl for bioinformatics
- Peal soap
- Schengenlyzeum perl
- Perl 5 regular expression
- Perl camel
- Perl text manipulation
- Perl linked list
- Perl ide
- Four perfect pebbles chapter 2
- Cgi linkage
- Cgi javatpoint
- Perl round
- Intro to perl
- Perl n
- Obfuscated perl
- Cgi perl
- Diamond perl
- Dan perl
- Perl conditionals
- Perl random number generator
- Perl bioinformatics
- Html program
- Perl println
- Php custom exception
- Cgi linkage in perl
- Perl read_file
- Perl modulus
- Perl bitwise operators
- "commercial" perl or tcl or python
- Aga perl
- Perlintro
- Perl log analysis
- Perl yaml example
- Perl shell scripting
- Perl tk tutorial
- Perl bioinformatics
- Perl logger
- Practical extraction and reporting language
- Swig perl
- Chromosome example in real life
- Perl hash table
- Perl web framework
- Piper perl
- The oneweek perl.com
- Perl paradigma
- Slidetodoc.com
- Java.util.random
- Apa perbedaan antara java swing dengan java awt
- Import.java.util.scanner;
- Java import java.util.*
- What is readline in java
- Import java.awt.* import java.awt.event.*
- Java import java.util.*
- Java import java.util.*
- Java import java.util.*
- Java compiler translates java source code into
- Java gcd
- Java import java.io.*
- Import java swing
- Rmi javatpoint
- Spring bean lifecycle
- Abstract window toolkit
- Java bicycle
- Sequential search java
- Tcp/ip sockets in java
- En java
- Java html to xml