SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Downloaden Sie, um offline zu lesen
Baby XS: Just enough to get you started
                   YAPC::NA 2012

                            Joel Berger

                    University of Illinois at Chicago


                          June 15, 2012




Joel Berger (UIC)                Baby XS                June 15, 2012   1 / 12
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
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
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
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
What are “baby” languages?


“Baby” Langauge
The 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
What are “baby” languages?


“Baby” Langauge
The 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
What are “baby” languages?


“Baby” Langauge
The 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
What are “baby” languages?


“Baby” Langauge
The 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
Baby XS


What is Baby XS?
Some “easy” idioms and rules-of-thumb to keep XS from becoming
overwhelming

    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
Baby XS


What is Baby XS?
Some “easy” idioms and rules-of-thumb to keep XS from becoming
overwhelming

    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
Baby XS


What is Baby XS?
Some “easy” idioms and rules-of-thumb to keep XS from becoming
overwhelming

    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
Baby XS


What is Baby XS?
Some “easy” idioms and rules-of-thumb to keep XS from becoming
overwhelming

    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
Baby XS


What is Baby XS?
Some “easy” idioms and rules-of-thumb to keep XS from becoming
overwhelming

    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
Baby XS


What is Baby XS?
Some “easy” idioms and rules-of-thumb to keep XS from becoming
overwhelming

    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
Baby XS


What is Baby XS?
Some “easy” idioms and rules-of-thumb to keep XS from becoming
overwhelming

    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
Types

XS 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 return
value
For Future Reference
You can add you own automatic conversions via a         Typemap   file


     Joel Berger (UIC)                Baby XS                       June 15, 2012   5 / 12
Types

XS 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 return
value
For Future Reference
You can add you own automatic conversions via a         Typemap   file


     Joel Berger (UIC)                Baby XS                       June 15, 2012   5 / 12
Types

XS 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 return
value
For Future Reference
You can add you own automatic conversions via a         Typemap   file


     Joel Berger (UIC)                Baby XS                       June 15, 2012   5 / 12
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::Module

int
meaning ()

void
greet (name)
    char* name




      Joel Berger (UIC)                 Baby XS   June 15, 2012   6 / 12
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 section
MODULE = My::Module       PACKAGE = My::Module

int
meaning ()

void
greet (name)
    char* name




      Joel Berger (UIC)                 Baby XS                      June 15, 2012   6 / 12
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 section
MODULE = My::Module       PACKAGE = My::Module

int
meaning ()

void
greet (name)
    char* name




      Joel Berger (UIC)                 Baby XS                       June 15, 2012   6 / 12
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 section
MODULE = My::Module       PACKAGE = My::Module

int
meaning ()

void
greet (name)
    char* name




      Joel Berger (UIC)                 Baby XS                       June 15, 2012   6 / 12
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 section
MODULE = My::Module       PACKAGE = My::Module

int
meaning ()                                          XS Code
                                                    (Shown: Simple Declarations)
void
greet (name)
    char* name




      Joel Berger (UIC)                 Baby XS                       June 15, 2012   6 / 12
Using Scalars (SV*s)

SV* behave like scalars in Perl, but have different actions based on type

Creating                                      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 Actions
Plenty 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
Using Scalars (SV*s)

SV* behave like scalars in Perl, but have different actions based on type

Creating                                      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 Actions
Plenty 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
Using Scalars (SV*s)

SV* behave like scalars in Perl, but have different actions based on type

Creating                                      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 Actions
Plenty 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
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
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
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
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
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
Sample Perl Module

package 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
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
Other C Connections



There 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
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

Weitere ähnliche Inhalte

Ähnlich wie Baby XS to get you started

Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted NewardAndroid | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted NewardJAX London
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScriptJorg Janke
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting languageVamshi Santhapuri
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesEelco Visser
 
Tips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software EngineeringTips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software Engineeringjtdudley
 
Unit 1-introduction to perl
Unit 1-introduction to perlUnit 1-introduction to perl
Unit 1-introduction to perlsana mateen
 
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesChoose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesCHOOSE
 
XML-Free Programming : Java Server and Client Development without <>
XML-Free Programming : Java Server and Client Development without <>XML-Free Programming : Java Server and Client Development without <>
XML-Free Programming : Java Server and Client Development without <>Arun Gupta
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)Ary Borenszweig
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)Ary Borenszweig
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)Crystal Language
 
Code Documentation. That ugly thing...
Code Documentation. That ugly thing...Code Documentation. That ugly thing...
Code Documentation. That ugly thing...Christos Manios
 
Streams of information - Chicago crystal language monthly meetup
Streams of information - Chicago crystal language monthly meetupStreams of information - Chicago crystal language monthly meetup
Streams of information - Chicago crystal language monthly meetupBrian Cardiff
 
Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Briefly Rust - Daniele Esposti - Codemotion Rome 2017Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Briefly Rust - Daniele Esposti - Codemotion Rome 2017Codemotion
 

Ähnlich wie Baby XS to get you started (20)

RDFa Tutorial
RDFa TutorialRDFa Tutorial
RDFa Tutorial
 
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted NewardAndroid | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
 
55j7
55j755j7
55j7
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting language
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific Languages
 
Tips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software EngineeringTips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software Engineering
 
Unit 1-introduction to perl
Unit 1-introduction to perlUnit 1-introduction to perl
Unit 1-introduction to perl
 
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesChoose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
 
XML-Free Programming : Java Server and Client Development without <>
XML-Free Programming : Java Server and Client Development without <>XML-Free Programming : Java Server and Client Development without <>
XML-Free Programming : Java Server and Client Development without <>
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 
Code Documentation. That ugly thing...
Code Documentation. That ugly thing...Code Documentation. That ugly thing...
Code Documentation. That ugly thing...
 
Kotlin L → ∞
Kotlin L → ∞Kotlin L → ∞
Kotlin L → ∞
 
Streams of information - Chicago crystal language monthly meetup
Streams of information - Chicago crystal language monthly meetupStreams of information - Chicago crystal language monthly meetup
Streams of information - Chicago crystal language monthly meetup
 
XML-Javascript
XML-JavascriptXML-Javascript
XML-Javascript
 
XML-Javascript
XML-JavascriptXML-Javascript
XML-Javascript
 
C to perl binding
C to perl bindingC to perl binding
C to perl binding
 
Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Briefly Rust - Daniele Esposti - Codemotion Rome 2017Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Briefly Rust - Daniele Esposti - Codemotion Rome 2017
 

Kürzlich hochgeladen

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 

Kürzlich hochgeladen (20)

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Baby XS to get you started

  • 1. Baby XS: Just enough to get you started YAPC::NA 2012 Joel Berger University of Illinois at Chicago June 15, 2012 Joel 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” Langauge The 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” Langauge The 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” Langauge The 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” Langauge The 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 XS What is Baby XS? Some “easy” idioms and rules-of-thumb to keep XS from becoming overwhelming 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 XS What is Baby XS? Some “easy” idioms and rules-of-thumb to keep XS from becoming overwhelming 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 XS What is Baby XS? Some “easy” idioms and rules-of-thumb to keep XS from becoming overwhelming 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 XS What is Baby XS? Some “easy” idioms and rules-of-thumb to keep XS from becoming overwhelming 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 XS What is Baby XS? Some “easy” idioms and rules-of-thumb to keep XS from becoming overwhelming 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 XS What is Baby XS? Some “easy” idioms and rules-of-thumb to keep XS from becoming overwhelming 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 XS What is Baby XS? Some “easy” idioms and rules-of-thumb to keep XS from becoming overwhelming 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. Types XS 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 return value For Future Reference You can add you own automatic conversions via a Typemap file Joel Berger (UIC) Baby XS June 15, 2012 5 / 12
  • 18. Types XS 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 return value For Future Reference You can add you own automatic conversions via a Typemap file Joel Berger (UIC) Baby XS June 15, 2012 5 / 12
  • 19. Types XS 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 return value For Future Reference You 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::Module int meaning () void greet (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 section MODULE = My::Module PACKAGE = My::Module int meaning () void greet (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 section MODULE = My::Module PACKAGE = My::Module int meaning () void greet (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 section MODULE = My::Module PACKAGE = My::Module int meaning () void greet (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 section MODULE = My::Module PACKAGE = My::Module int meaning () XS Code (Shown: Simple Declarations) void greet (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 type Creating 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 Actions Plenty 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 type Creating 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 Actions Plenty 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 type Creating 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 Actions Plenty 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 Module package 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 Connections There 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