Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing Perl Extensions: There’s more than one way to do it.

Similar presentations


Presentation on theme: "Writing Perl Extensions: There’s more than one way to do it."— Presentation transcript:

1 Writing Perl Extensions: There’s more than one way to do it.

2 Why write an extension? speed reuse legacy code or libraries written in other languages add new language features example: The Perl Data Language

3 What is an extension? Compiled code which can be loaded (usually dynamically) into a Perl program. your_extension.pm:... require DynaLoader; bootstrap your_extension;... loads shared library -- @INC/auto/your_extension/libyour_extension.{ a so dll } Now functions from that shared library can be called as you would call Perl functions.

4 Methods of writing an extension. write raw C code yourself use XS to glue your C code and perl use swig to create multi-language glue use Inline::C to do *all* the work for you use PDL::PP to create PDL aware XS code

5 what we’re going to do. Write and extension which executes C code which looks like this: char * hello(char * who) { char * message = malloc(...); sprintf(message,”Hello %s\n”,who); return message; } Which does the equivilant of sub hello { return “Hello $_[0]\n”; }

6 Write your extension manually in C. #include "EXTERN.h" #include "perl.h" #include "XSUB.h" XS(XS_your_extension_hello) { dXSARGS; if (items != 1) Perl_croak(aTHX_ "Usage: your_extension::hello(who)"); { char *who = (char *)SvPV(ST(0),PL_na); char *RETVAL; dXSTARG; RETVAL = malloc(100 * (sizeof (char))); sprintf(RETVAL, "hello %s\n",who); sv_setpv(TARG, RETVAL); XSprePUSH; PUSHTARG; } XSRETURN(1); } XS(boot_your_extension) { dXSARGS; char* file = __FILE__; XS_VERSION_BOOTCHECK ; newXS("your_extension::hello", XS_your_extension_hello, file); XSRETURN_YES; }

7 Write a.xs file and use xsubpp to do most of the work for you. #include "EXTERN.h" #include "perl.h" #include "XSUB.h" MODULE = your_extensionPACKAGE = your_extension char * hello(who); char * who; CODE: RETVAL = malloc(100 * (sizeof (char))); sprintf(RETVAL, "hello %s\n",who); OUTPUT: RETVAL

8 XS has a learning curve... perldoc perlxs and perlxstut (the xs tutorial) to learn lots of crazy things about.xs files. knowing perlguts and perlapi also helps.

9 Use h2xs to make your.xs file from a header file. h2xs will scan a C.h file and do the right thing ™ to create the appropriate glue code to link your C functions and Perl. Always start with h2xs anyway, even if you’re not using xs -- because it sets up a Makefile.PL.

10 Use swig to create simpler, multi-language interfaces. xs is very specific to Perl and its internals, you end up getting your hands dirty with it… useful sometimes but not always necessary. swig extensions can be easily be retarget’d to Python, Scheme, Guile, Tcl and Java.

11 SWIG: Simplified Wrapper and Interface Generator >swig -perl5 example.i creates => example.pm, example_wrap.c, example.doc Must compile manually or use MakeMaker.pm to write a Makefile for you. >gcc example.c # compile your file >gcc example_wrap.c -I >ld -shared example.o example_wrap.o -o example.so

12 What SWIG does for you. Your C functions can be now called from perl. You can specify default arguments in the interface file. Global variables and #defined constants become package variables. ----example.i--------- example.pm ----- %module example use example int foo(int a); &example::foo(5) void bar(double, double b=3.0);&example:bar(4.4) double foobar;$example::foobar #define barfoo 5$example::barfoo

13 SWIG features. doubles, ints, char *, etc. get passed around as you would expect (and hope). pointers get passed to and from perl as blessed references ($$ref is C pointer). special magic exists for accessing C arrays. perl “shadow classes” may be used interface to C structs and C++ classes.

14 Make your life simpler with Inline use Inline C; print "9 + 16 = ", add(9, 16), "\n"; print "9 - 16 = ", subtract(9, 16), "\n"; __END__ __C__ int add(int x, int y) { return x + y; } int subtract(int x, int y) { return x - y; }

15 more Inline perl -e 'use Inline C=>q/void greet(){printf("Hello, world\n");}/;greet'

16 Inline.pm uses Inline::.pm to do all the work based on the language you specify. supports: passing of various data types, multiple return values, variable argument lists, and if you’re really crazy, evaluation of C code at runtime.

17 A few short words about the PDL preprocessor. In the file example.pd: pp_def( ‘sumover’, Pars => ‘a(n); [o] b();’, Code => ‘ double tmp=0; loop(n) %{ tmp += $a(); %} $b() = tmp;’ ); There’s also an Inline::PDLpp version.


Download ppt "Writing Perl Extensions: There’s more than one way to do it."

Similar presentations


Ads by Google