Baby XS to get you started

  • 1,035 views
Uploaded on

Presented at YAPC::NA 2012. Conference site: http://act.yapcna.org/2012/talk/78 …

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,035
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
6
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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