SlideShare une entreprise Scribd logo
1  sur  36
Télécharger pour lire hors ligne
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

Contenu connexe

Similaire à 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
 

Similaire à 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
 

Dernier

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 

Dernier (20)

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 

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