0
Baby XS: Just enough to get you started                   YAPC::NA 2012                            Joel Berger            ...
What is XS?    XS the mechanism used to connect Perl and C           write optimized functions           access C librarie...
What is XS?    XS the mechanism used to connect Perl and C           write optimized functions           access C librarie...
What is XS?    XS the mechanism used to connect Perl and C           write optimized functions           access C librarie...
What is XS?    XS the mechanism used to connect Perl and C           write optimized functions           access C librarie...
What are “baby” languages?“Baby” LangaugeThe naive code that new programmers write,a simple subset of the full language   ...
What are “baby” languages?“Baby” LangaugeThe naive code that new programmers write,a simple subset of the full language   ...
What are “baby” languages?“Baby” LangaugeThe naive code that new programmers write,a simple subset of the full language   ...
What are “baby” languages?“Baby” LangaugeThe naive code that new programmers write,a simple subset of the full language   ...
Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming    ignores typemaps    u...
Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming    ignores typemaps    u...
Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming    ignores typemaps    u...
Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming    ignores typemaps    u...
Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming    ignores typemaps    u...
Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming    ignores typemaps    u...
Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming    ignores typemaps    u...
TypesXS has types that are like their Perl counterparts     Scalar ⇔      SV*     Array ⇔      AV*     Hash ⇔       HV*Of ...
TypesXS has types that are like their Perl counterparts     Scalar ⇔      SV*     Array ⇔      AV*     Hash ⇔       HV*Of ...
TypesXS has types that are like their Perl counterparts     Scalar ⇔      SV*     Array ⇔      AV*     Hash ⇔       HV*Of ...
Sample XS File# include "EXTERN.h"# include "perl.h"# include "XSUB.h"int meaning () { return 42 }void greet (char* name){...
Sample XS File# include "EXTERN.h"# include "perl.h"# include "XSUB.h"int meaning () { return 42 }void greet (char* name){...
Sample XS File# include "EXTERN.h"                                                    XS standard inclusions# include "per...
Sample XS File# include "EXTERN.h"                                                    XS standard inclusions# include "per...
Sample XS File# include "EXTERN.h"                                                    XS standard inclusions# include "per...
Using Scalars (SV*s)SV* behave like scalars in Perl, but have different actions based on typeCreating                      ...
Using Scalars (SV*s)SV* behave like scalars in Perl, but have different actions based on typeCreating                      ...
Using Scalars (SV*s)SV* behave like scalars in Perl, but have different actions based on typeCreating                      ...
Using Arrays (AV*s) and Multiple Returns       Perl-like functions, e.g.      av push       filled with     SV*   objects  ...
Using Arrays (AV*s) and Multiple Returns       Perl-like functions, e.g.      av push       filled with     SV*   objects  ...
Using Arrays (AV*s) and Multiple Returns       Perl-like functions, e.g.      av push       filled with     SV*   objects  ...
Using Arrays (AV*s) and Multiple Returns       Perl-like functions, e.g.      av push       filled with     SV*   objects  ...
Using Arrays (AV*s) and Multiple Returns       Perl-like functions, e.g.      av push       filled with     SV*   objects  ...
Sample Perl Modulepackage MyModule;use strict; use warnings;our $VERSION = ’0.01’;require XSLoader;XSLoader::load();sub my...
Building/Packaging (Module::Build)Structure                                  Build.PL                                     ...
Other C ConnectionsThere are other mechanisms for hooking into C    Inline::C           write C in your Perl script       ...
Acknowledgements   Graduate College Dean’s Fellowship (Major Funding)   LAS Ph.D. Travel Award (Conference Funding)https:/...
Upcoming SlideShare
Loading in...5
×

Baby XS to get you started

1,253

Published on

Presented at YAPC::NA 2012. Conference site: http://act.yapcna.org/2012/talk/78
github repo: https://github.com/jberger/YAPCNA2012/tree/master/BabyXS

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,253
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
9
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Baby XS to get you started"

  1. 1. Baby XS: Just enough to get you started YAPC::NA 2012 Joel Berger University of Illinois at Chicago June 15, 2012Joel Berger (UIC) Baby XS June 15, 2012 1 / 12
  2. 2. What is XS? XS the mechanism used to connect Perl and C write optimized functions access C libraries #!/usr/bin/perl XS #include ‘‘headers’’ my $var = from c() ? int from c () { ... }But what is XS? XS is C functions/macros provided by Perl headers XS is also XS-preprocessesor directives Joel Berger (UIC) Baby XS June 15, 2012 2 / 12
  3. 3. What is XS? XS the mechanism used to connect Perl and C write optimized functions access C libraries #!/usr/bin/perl XS #include ‘‘headers’’ my $var = from c() ? int from c () { ... }But what is XS? XS is C functions/macros provided by Perl headers XS is also XS-preprocessesor directives Joel Berger (UIC) Baby XS June 15, 2012 2 / 12
  4. 4. What is XS? XS the mechanism used to connect Perl and C write optimized functions access C libraries #!/usr/bin/perl XS #include ‘‘headers’’ my $var = from c() ? int from c () { ... }But what is XS? XS is C functions/macros provided by Perl headers XS is also XS-preprocessesor directives Joel Berger (UIC) Baby XS June 15, 2012 2 / 12
  5. 5. What is XS? XS the mechanism used to connect Perl and C write optimized functions access C libraries #!/usr/bin/perl XS #include ‘‘headers’’ my $var = from c() ? int from c () { ... }But what is XS? XS is C functions/macros provided by Perl headers XS is also XS-preprocessesor directives Joel Berger (UIC) Baby XS June 15, 2012 2 / 12
  6. 6. What are “baby” languages?“Baby” LangaugeThe naive code that new programmers write,a simple subset of the full language “Baby Perl” no use of map / grep avoids references avoids $ and other special variables “Full XS” is powerful but is lots to learn “Baby XS” looks like C behaves like Perl Joel Berger (UIC) Baby XS June 15, 2012 3 / 12
  7. 7. What are “baby” languages?“Baby” LangaugeThe naive code that new programmers write,a simple subset of the full language “Baby Perl” no use of map / grep avoids references avoids $ and other special variables “Full XS” is powerful but is lots to learn “Baby XS” looks like C behaves like Perl Joel Berger (UIC) Baby XS June 15, 2012 3 / 12
  8. 8. What are “baby” languages?“Baby” LangaugeThe naive code that new programmers write,a simple subset of the full language “Baby Perl” no use of map / grep avoids references avoids $ and other special variables “Full XS” is powerful but is lots to learn “Baby XS” looks like C behaves like Perl Joel Berger (UIC) Baby XS June 15, 2012 3 / 12
  9. 9. What are “baby” languages?“Baby” LangaugeThe naive code that new programmers write,a simple subset of the full language “Baby Perl” no use of map / grep avoids references avoids $ and other special variables “Full XS” is powerful but is lots to learn “Baby XS” looks like C behaves like Perl Joel Berger (UIC) Baby XS June 15, 2012 3 / 12
  10. 10. Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming ignores typemaps uses Perl datatype-specific functions from perldoc perlguts ignores most of the XSpp commands uses a Perl-level function wrapper to munge input/output if needed ignores stack macros avoids (most) issues of “mortalization” expands to “real” XS Joel Berger (UIC) Baby XS June 15, 2012 4 / 12
  11. 11. Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming ignores typemaps uses Perl datatype-specific functions from perldoc perlguts ignores most of the XSpp commands uses a Perl-level function wrapper to munge input/output if needed ignores stack macros avoids (most) issues of “mortalization” expands to “real” XS Joel Berger (UIC) Baby XS June 15, 2012 4 / 12
  12. 12. Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming ignores typemaps uses Perl datatype-specific functions from perldoc perlguts ignores most of the XSpp commands uses a Perl-level function wrapper to munge input/output if needed ignores stack macros avoids (most) issues of “mortalization” expands to “real” XS Joel Berger (UIC) Baby XS June 15, 2012 4 / 12
  13. 13. Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming ignores typemaps uses Perl datatype-specific functions from perldoc perlguts ignores most of the XSpp commands uses a Perl-level function wrapper to munge input/output if needed ignores stack macros avoids (most) issues of “mortalization” expands to “real” XS Joel Berger (UIC) Baby XS June 15, 2012 4 / 12
  14. 14. Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming ignores typemaps uses Perl datatype-specific functions from perldoc perlguts ignores most of the XSpp commands uses a Perl-level function wrapper to munge input/output if needed ignores stack macros avoids (most) issues of “mortalization” expands to “real” XS Joel Berger (UIC) Baby XS June 15, 2012 4 / 12
  15. 15. Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming ignores typemaps uses Perl datatype-specific functions from perldoc perlguts ignores most of the XSpp commands uses a Perl-level function wrapper to munge input/output if needed ignores stack macros avoids (most) issues of “mortalization” expands to “real” XS Joel Berger (UIC) Baby XS June 15, 2012 4 / 12
  16. 16. Baby XSWhat is Baby XS?Some “easy” idioms and rules-of-thumb to keep XS from becomingoverwhelming ignores typemaps uses Perl datatype-specific functions from perldoc perlguts ignores most of the XSpp commands uses a Perl-level function wrapper to munge input/output if needed ignores stack macros avoids (most) issues of “mortalization” expands to “real” XS Joel Berger (UIC) Baby XS June 15, 2012 4 / 12
  17. 17. TypesXS has types that are like their Perl counterparts Scalar ⇔ SV* Array ⇔ AV* Hash ⇔ HV*Of course XS is really C so it also has types like int double char*. . . which Perl converts to/from SV* when used as arguments or returnvalueFor Future ReferenceYou can add you own automatic conversions via a Typemap file Joel Berger (UIC) Baby XS June 15, 2012 5 / 12
  18. 18. TypesXS has types that are like their Perl counterparts Scalar ⇔ SV* Array ⇔ AV* Hash ⇔ HV*Of course XS is really C so it also has types like int double char*. . . which Perl converts to/from SV* when used as arguments or returnvalueFor Future ReferenceYou can add you own automatic conversions via a Typemap file Joel Berger (UIC) Baby XS June 15, 2012 5 / 12
  19. 19. TypesXS has types that are like their Perl counterparts Scalar ⇔ SV* Array ⇔ AV* Hash ⇔ HV*Of course XS is really C so it also has types like int double char*. . . which Perl converts to/from SV* when used as arguments or returnvalueFor Future ReferenceYou can add you own automatic conversions via a Typemap file Joel Berger (UIC) Baby XS June 15, 2012 5 / 12
  20. 20. Sample XS File# include "EXTERN.h"# include "perl.h"# include "XSUB.h"int meaning () { return 42 }void greet (char* name){ printf( "Hello %sn", name )}MODULE = My::Module PACKAGE = My::Moduleintmeaning ()voidgreet (name) char* name Joel Berger (UIC) Baby XS June 15, 2012 6 / 12
  21. 21. Sample XS File# include "EXTERN.h"# include "perl.h"# include "XSUB.h"int meaning () { return 42 }void greet (char* name){ printf( "Hello %sn", name )} Begin XS sectionMODULE = My::Module PACKAGE = My::Moduleintmeaning ()voidgreet (name) char* name Joel Berger (UIC) Baby XS June 15, 2012 6 / 12
  22. 22. Sample XS File# include "EXTERN.h" XS standard inclusions# include "perl.h" (loads standard C headers)# include "XSUB.h"int meaning () { return 42 }void greet (char* name){ printf( "Hello %sn", name )} Begin XS sectionMODULE = My::Module PACKAGE = My::Moduleintmeaning ()voidgreet (name) char* name Joel Berger (UIC) Baby XS June 15, 2012 6 / 12
  23. 23. Sample XS File# include "EXTERN.h" XS standard inclusions# include "perl.h" (loads standard C headers)# include "XSUB.h"int meaning () { return 42 }void greet (char* name) C Code{ printf( "Hello %sn", name )} Begin XS sectionMODULE = My::Module PACKAGE = My::Moduleintmeaning ()voidgreet (name) char* name Joel Berger (UIC) Baby XS June 15, 2012 6 / 12
  24. 24. Sample XS File# include "EXTERN.h" XS standard inclusions# include "perl.h" (loads standard C headers)# include "XSUB.h"int meaning () { return 42 }void greet (char* name) C Code{ printf( "Hello %sn", name )} Begin XS sectionMODULE = My::Module PACKAGE = My::Moduleintmeaning () XS Code (Shown: Simple Declarations)voidgreet (name) char* name Joel Berger (UIC) Baby XS June 15, 2012 6 / 12
  25. 25. Using Scalars (SV*s)SV* behave like scalars in Perl, but have different actions based on typeCreating Accessing SV* newSViv(IV) int SvIV(SV*) SV* newSVnv(double) double SvNV(SV*) SV* newSVpvf(const char*, ...) char* SvPV(SV*, STRLEN len) SV* newSVsv(SV*) char* SvPV nolen(SV*)Other ActionsPlenty of other Perl-like actions, see perldoc perlguts for more. SvTRUE(SV*) — test for “truth” sv catsv(SV*, SV*) — join strings Joel Berger (UIC) Baby XS June 15, 2012 7 / 12
  26. 26. Using Scalars (SV*s)SV* behave like scalars in Perl, but have different actions based on typeCreating Accessing SV* newSViv(IV) int SvIV(SV*) SV* newSVnv(double) double SvNV(SV*) SV* newSVpvf(const char*, ...) char* SvPV(SV*, STRLEN len) SV* newSVsv(SV*) char* SvPV nolen(SV*)Other ActionsPlenty of other Perl-like actions, see perldoc perlguts for more. SvTRUE(SV*) — test for “truth” sv catsv(SV*, SV*) — join strings Joel Berger (UIC) Baby XS June 15, 2012 7 / 12
  27. 27. Using Scalars (SV*s)SV* behave like scalars in Perl, but have different actions based on typeCreating Accessing SV* newSViv(IV) int SvIV(SV*) SV* newSVnv(double) double SvNV(SV*) SV* newSVpvf(const char*, ...) char* SvPV(SV*, STRLEN len) SV* newSVsv(SV*) char* SvPV nolen(SV*)Other ActionsPlenty of other Perl-like actions, see perldoc perlguts for more. SvTRUE(SV*) — test for “truth” sv catsv(SV*, SV*) — join strings Joel Berger (UIC) Baby XS June 15, 2012 7 / 12
  28. 28. Using Arrays (AV*s) and Multiple Returns Perl-like functions, e.g. av push filled with SV* objects used as argument or return value, Perl uses references “mortalization” problem for returns recommended Baby XS way to return multiple values:AV* foo () { AV* ret = newAV(); /* fix mortalization */ sv_2mortal((SV*)ret); av_push(ret, newSViv(1)); av_push(ret, newSVpvf("%s", "bar")); /* return [ 1, "bar" ] */ return ret;} Joel Berger (UIC) Baby XS June 15, 2012 8 / 12
  29. 29. Using Arrays (AV*s) and Multiple Returns Perl-like functions, e.g. av push filled with SV* objects used as argument or return value, Perl uses references “mortalization” problem for returns recommended Baby XS way to return multiple values:AV* foo () { AV* ret = newAV(); /* fix mortalization */ sv_2mortal((SV*)ret); av_push(ret, newSViv(1)); av_push(ret, newSVpvf("%s", "bar")); /* return [ 1, "bar" ] */ return ret;} Joel Berger (UIC) Baby XS June 15, 2012 8 / 12
  30. 30. Using Arrays (AV*s) and Multiple Returns Perl-like functions, e.g. av push filled with SV* objects used as argument or return value, Perl uses references “mortalization” problem for returns recommended Baby XS way to return multiple values:AV* foo () { AV* ret = newAV(); /* fix mortalization */ sv_2mortal((SV*)ret); av_push(ret, newSViv(1)); av_push(ret, newSVpvf("%s", "bar")); /* return [ 1, "bar" ] */ return ret;} Joel Berger (UIC) Baby XS June 15, 2012 8 / 12
  31. 31. Using Arrays (AV*s) and Multiple Returns Perl-like functions, e.g. av push filled with SV* objects used as argument or return value, Perl uses references “mortalization” problem for returns recommended Baby XS way to return multiple values:AV* foo () { AV* ret = newAV(); /* fix mortalization */ sv_2mortal((SV*)ret); av_push(ret, newSViv(1)); av_push(ret, newSVpvf("%s", "bar")); /* return [ 1, "bar" ] */ return ret;} Joel Berger (UIC) Baby XS June 15, 2012 8 / 12
  32. 32. Using Arrays (AV*s) and Multiple Returns Perl-like functions, e.g. av push filled with SV* objects used as argument or return value, Perl uses references “mortalization” problem for returns recommended Baby XS way to return multiple values:AV* foo () { AV* ret = newAV(); /* fix mortalization */ sv_2mortal((SV*)ret); av_push(ret, newSViv(1)); av_push(ret, newSVpvf("%s", "bar")); /* return [ 1, "bar" ] */ return ret;} Joel Berger (UIC) Baby XS June 15, 2012 8 / 12
  33. 33. Sample Perl Modulepackage MyModule;use strict; use warnings;our $VERSION = ’0.01’;require XSLoader;XSLoader::load();sub myfunc { my @args = @_; my $ret = c_func(@args); return wantarray ? @$ret : $ret->[0];} Wrap C function calls in Perl subs munge inputs / outputs easier abstraction if C function changes Export the Perl function (if desired) Joel Berger (UIC) Baby XS June 15, 2012 9 / 12
  34. 34. Building/Packaging (Module::Build)Structure Build.PL use strict;root folder use warnings; use Module::Build; lib my $builder = Module::Build->new( module_name => ’MyModule’, MyModule.pm dist_author => ’Joel Berger’, license => ’perl’, configure_requires => { MyModule.xs ’Module::Build’ => 0.38, }, build_requires => { Build.PL ’ExtUtils::CBuilder’ => 0, }, extra_linker_flags => ’-lsomelib’, ... ); $builder->create_build_script; Joel Berger (UIC) Baby XS June 15, 2012 10 / 12
  35. 35. Other C ConnectionsThere are other mechanisms for hooking into C Inline::C write C in your Perl script builds/loads the XS for you! great for quick checks PDL::PP part of PDL special C interaction for fast numerical processing has its own syntax Joel Berger (UIC) Baby XS June 15, 2012 11 / 12
  36. 36. Acknowledgements Graduate College Dean’s Fellowship (Major Funding) LAS Ph.D. Travel Award (Conference Funding)https://github.com/jberger/YAPCNA2012 Joel Berger (UIC) Baby XS June 15, 2012 12 / 12
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×