SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Downloaden Sie, um offline zu lesen
C Perl, C Perl Inline, C Perl XS, C Perl Guts                            Abram Hindle




                         C Perl, C Perl Inline, C Perl XS, C Perl Guts

                                            Abram Hindle

                              Victoria Perl Mongers (victoria.pm.org)

                                         abez@abez.ca

                                           March 16, 2004




Victoria.pm                                                                        1
C Perl, C Perl Inline, C Perl XS, C Perl Guts   Abram Hindle




        This Presentation
          • What am I going to Cover?
              – Inline::C

              – Perl Data Structures / Guts

              – XS




Victoria.pm                                               2
C Perl, C Perl Inline, C Perl XS, C Perl Guts                                  Abram Hindle




        This Presentation
          • What am I not going to Cover?
              – How to program in C

              – C++

              – Anything in depth

              – Magic Perl Guts

              – OO and Perl Guts

              – References and Perl Guts

              – SWIG (maybe you should take a look if you’re wrapping a lib)




Victoria.pm                                                                              3
C Perl, C Perl Inline, C Perl XS, C Perl Guts                       Abram Hindle




        Inline::C
          • What is it?
              – Write Perl Subroutines in C [1]

              – Compile and Link C into Perl

              – Embed subroutines into perl scripts and modules

              – Automatically Wrap functions based on prototypes.




Victoria.pm                                                                   4
C Perl, C Perl Inline, C Perl XS, C Perl Guts       Abram Hindle




        Inline::C
          • How do you use Inline::C?
              – use Inline C => CODE;

              – bind Inline C => CODE;
                  ∗ Dynamic generation of C code.
              –   use Inline C;
                  __END__
                  __C__
                  void cCode() {
                  }




Victoria.pm                                                   5
C Perl, C Perl Inline, C Perl XS, C Perl Guts                                        Abram Hindle




        Inline::C
          • How do you use Inline::C?
              – Inline::C parses the function prototypes and wraps them into perl.

              – Function prototypes should be in ANSI style
                ∗ return-type function-name ( type-name-pairs ) ...




Victoria.pm                                                                                    6
C Perl, C Perl Inline, C Perl XS, C Perl Guts                  Abram Hindle




        Inline::C
          • Which types are supported?
              – void → nothing

              – double → scalar

              – char * → string

              – SV* → Scalar

              – long → Scalar

              – float is not supported but you can support it




Victoria.pm                                                              7
C Perl, C Perl Inline, C Perl XS, C Perl Guts   Abram Hindle




        Inline::C
          • Example
              use Inline C;
              helloWorld();
              __END__
              __C__
              void helloWorld() {
                printf("Hello worldn");
              }




Victoria.pm                                               8
C Perl, C Perl Inline, C Perl XS, C Perl Guts   Abram Hindle




        Inline::C
          • Example
              use Inline C;
              hello(’World’);
              hello(42);
              hello(42,42);
              hello(undef);
              __END__
              __C__
              void hello(char * string) {
                printf("Hello %sn",string);
              }




Victoria.pm                                               9
C Perl, C Perl Inline, C Perl XS, C Perl Guts                                      Abram Hindle




        Inline::C
          • Example (perl guts)
              use Inline C;
              $a = "HEY";
              hello(’World’,1,42,undef,’Universe’,$a);
              __END__
              __C__
              void hello(SV* name1, ...) {
                      Inline_Stack_Vars;
                      int i = 0;
                      STRLEN count;
                      for (i = 0; i < Inline_Stack_Items; i++) {
                              char * string = SvPV(Inline_Stack_Item(i), count);
                              printf("Hello %sn",string);
                      }
              }




Victoria.pm                                                                                 10
C Perl, C Perl Inline, C Perl XS, C Perl Guts                                                  Abram Hindle




        Inline::C
          • Inline Stack Access macros
              – Inline Stack Vars       - put this macros at the top of the subroutine as it
                defines new various for Inline Stack macros to work on

              – Inline Stack Items       - how many items are on the stack

              – Inline Stack Item(i)       - get the item at index i in the stack

              – see perldoc Inline::C




Victoria.pm                                                                                             11
C Perl, C Perl Inline, C Perl XS, C Perl Guts   Abram Hindle




        Perl Guts
          • Types in C [5]
              – SV - Scalar Value

              – AV - Array Value

              – HV - Hash Value

              – IV - Integer Value *

              – UV - Unsigned Value *

              – NV - Double Value *

              – PV - String Value *

              – * return values




Victoria.pm                                              12
C Perl, C Perl Inline, C Perl XS, C Perl Guts                 Abram Hindle




        Perl Guts
          • Convert Scalars [5] ( these are macros )
              – int SvIV(SV*)

              – unsigned int SvUV(SV*)

              – double SvNV(SV*)

              – char * SvPV(SV*, STRLEN len)
               ∗ puts length in len, i is the buffer in SV*
              – char * SvPV nolen(SV*)




Victoria.pm                                                            13
C Perl, C Perl Inline, C Perl XS, C Perl Guts           Abram Hindle




        Perl Guts
          • Set scalars [5] ( these are macros )
              – void sv setiv(SV*, IV);

              – void sv setuv(SV*, UV);

              – void sv setnv(SV*, double);

              – void sv setpv(SV*, const char*);

              – void sv setpvn(SV*, const char*, int)




Victoria.pm                                                      14
C Perl, C Perl Inline, C Perl XS, C Perl Guts         Abram Hindle




        Perl Guts
          • Create scalars [5] ( these are macros )
              – SV* newSViv(IV);

              – SV* newSVuv(UV);

              – SV* newSVnv(double);

              – SV* newSVpv(const char*, int);

              – SV* newSVpvn(const char*, int);

              – SV* newSVpvf(const char*, ...);

              – SV* newSVsv(SV*);




Victoria.pm                                                    15
C Perl, C Perl Inline, C Perl XS, C Perl Guts                  Abram Hindle




        Perl Guts
          • Arrays [5]
              – Make an array
               ∗ create -    AV* newAV();

               ∗ create -    AV* av make(I32 num, SV **ptr);

              – Operations
               ∗   push - void av push(AV*, SV*);
               ∗   pop - SV* av pop(AV*);
               ∗   shift - SV* av shift(AV*);
               ∗   unshift - void av unshift(AV*, I32 num);




Victoria.pm                                                             16
C Perl, C Perl Inline, C Perl XS, C Perl Guts                    Abram Hindle




        Perl Guts
          • Arrays [5]
              – length - I32 av len(AV*);

              – fetch - SV** av fetch(AV*, I32 key, I32 lval);

              – store - SV** av store(AV*, I32 key, SV* val);

              – clear - void av clear(AV*);

              – destroy - void av undef(AV*);

              – expand - void av extend(AV*, I32 key);




Victoria.pm                                                               17
C Perl, C Perl Inline, C Perl XS, C Perl Guts                                   Abram Hindle




        Perl Guts
          • Hashes
              – HV* newHV();

              – SV** hv store(HV*, const char* key, U32 klen, SV* val, U32 hash);

              – SV** hv fetch(HV*, const char* key, U32 klen, I32 lval);

              – bool hv exists(HV*, const char* key, U32 klen);

              – SV* hv delete(HV*, const char* key, U32 klen, I32 flags);

              – void hv clear(HV*);

              – void hv undef(HV*);




Victoria.pm                                                                              18
C Perl, C Perl Inline, C Perl XS, C Perl Guts                      Abram Hindle




        Perl Guts
          • Hash iteration
              – I32 hv iterinit(HV*);

              – HE* hv iternext(HV*);

              – char* hv iterkey(HE* entry, I32* retlen);

              – SV* hv iterval(HV*, HE* entry);

              – SV* hv iternextsv(HV*, char** key, I32* retlen);




Victoria.pm                                                                 19
C Perl, C Perl Inline, C Perl XS, C Perl Guts           Abram Hindle




        Perl Guts
          • Package Vars [5]
              – SV* get sv("package::varname", TRUE);

              – AV* get av("package::varname", TRUE);

              – HV* get hv("package::varname", TRUE);




Victoria.pm                                                      20
C Perl, C Perl Inline, C Perl XS, C Perl Guts                                          Abram Hindle




        Perl Guts
          • Mortality
              – If you have temporary values such as those that are returned or used
                temporarily you want the garbage collection to handle it properley.

              – SV* sv newmortal()

              – SV* sv 2mortal(SV* sv)




Victoria.pm                                                                                     21
C Perl, C Perl Inline, C Perl XS, C Perl Guts                    Abram Hindle




        XS
          • What is XS?
              – interface description format

              – Integrate Perl and C code / libs

              – A language that is used to integrate C w/ Perl

              – Wraps C calls




Victoria.pm                                                               22
C Perl, C Perl Inline, C Perl XS, C Perl Guts                                         Abram Hindle




        XS
          • Into the fire: [4] - Some of this is ripped from the perlxstut
              – h2xs -A -n PackageName

              – Edit PackageName.xs

              – perl Makefile.PL

              – make

              – Make a test script in the PackageName directory

              – make install – only do this if your test script has problems finding
                PackageName




Victoria.pm                                                                                    23
C Perl, C Perl Inline, C Perl XS, C Perl Guts                                       Abram Hindle




        XS
          • The .xs file
              – Prototypes are done differently, see perldoc perlxs and perlxstut

              – First line, return type

              – Second line, prototype in K&R

              – Following lines, type the parameters (newline is the delimiter)

              –   int
                  strcmp(str1,str2)
                          char *str1
                          char *str2




Victoria.pm                                                                                  24
C Perl, C Perl Inline, C Perl XS, C Perl Guts                              Abram Hindle




        XS
          • Lets wrap a already existing function (add this to your .xs)
              – strcmp

              –   #include <string.h>
                  int
                  strcmp(str1,str2)
                          char *str1
                          char *str2
              – That’s it




Victoria.pm                                                                         25
C Perl, C Perl Inline, C Perl XS, C Perl Guts                  Abram Hindle




        XS
          • Lets write a new function (add this to your .xs)
              – hello

              –   void
                  hello()
                  CODE:
                  printf("Hello Worldn");
              – That’s it




Victoria.pm                                                             26
C Perl, C Perl Inline, C Perl XS, C Perl Guts                      Abram Hindle




        XS
          • Lets see our test driver (after make install)
                  #!/usr/bin/perl
                  use Hello;

                  Hello::hello();

                  print join(" ",
                                    Hello::strcmp("a","b"),
                                    Hello::strcmp("a","a"),
                                    Hello::strcmp("bbbb","aaaa")
                          ),$/;

              –




Victoria.pm                                                                 27
C Perl, C Perl Inline, C Perl XS, C Perl Guts   Abram Hindle




        Get Help
          • Where to get Help?
              – perldoc Inline

              – perldoc Inline::C

              – perldoc Inline::C-Cookbook

              – perldoc perlguts

              – perldoc perlapi

              – perldoc perlxs

              – perldoc perlxstut

              – perldoc perlembed




Victoria.pm                                              28
C Perl, C Perl Inline, C Perl XS, C Perl Guts                                             Abram Hindle



        References

        [1] I NGERSON , B. Inline::c - write perl subroutines in c.

        [2] I NGERSON , B., AND WATKISS , N. Inline - write perl subroutines in other
              programming languages.

        [3] M AC E ACHERN , D., AND O RWANT, J. perlembed - how to embed perl in your c
              program.

        [4] O KAMOTO, J. perlxstut - tutorial for writing xsubs.

        [5] P ORTERS , P. . perlguts - introduction to the perl api.

        [6] R OEHRICH , D., O KAMOTO, J., AND S TUHL , B. perlapi - autogenerated
              documentation for the perl public api.

        [7] R OEHRICH , D., AND P ORTERS , T. P. perlxs - xs language reference manual.



Victoria.pm                                                                                        29

Weitere ähnliche Inhalte

Was ist angesagt?

Understanding Javascript Engines
Understanding Javascript Engines Understanding Javascript Engines
Understanding Javascript Engines Parashuram N
 
What's New in ES6 for Web Devs
What's New in ES6 for Web DevsWhat's New in ES6 for Web Devs
What's New in ES6 for Web DevsRami Sayar
 
IronSmalltalk
IronSmalltalkIronSmalltalk
IronSmalltalkESUG
 
Denker - Pharo: Present and Future - 2009-07-14
Denker - Pharo: Present and Future - 2009-07-14Denker - Pharo: Present and Future - 2009-07-14
Denker - Pharo: Present and Future - 2009-07-14CHOOSE
 
JRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVMJRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVMCharles Nutter
 
I Know Kung Fu - Juggling Java Bytecode
I Know Kung Fu - Juggling Java BytecodeI Know Kung Fu - Juggling Java Bytecode
I Know Kung Fu - Juggling Java BytecodeAlexander Shopov
 
ROP 輕鬆談
ROP 輕鬆談ROP 輕鬆談
ROP 輕鬆談hackstuff
 
Implementing a JavaScript Engine
Implementing a JavaScript EngineImplementing a JavaScript Engine
Implementing a JavaScript EngineKris Mok
 
Ruby Performance - The Last Mile - RubyConf India 2016
Ruby Performance - The Last Mile - RubyConf India 2016Ruby Performance - The Last Mile - RubyConf India 2016
Ruby Performance - The Last Mile - RubyConf India 2016Charles Nutter
 
05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR matters05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR mattersAlexandre Moneger
 
Triton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON ChinaTriton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON ChinaWei-Bo Chen
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]RootedCON
 
(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-stringsNico Ludwig
 
JRuby 9000 - Taipei Ruby User's Group 2015
JRuby 9000 - Taipei Ruby User's Group 2015JRuby 9000 - Taipei Ruby User's Group 2015
JRuby 9000 - Taipei Ruby User's Group 2015Charles Nutter
 
Why TypeScript?
Why TypeScript?Why TypeScript?
Why TypeScript?FITC
 

Was ist angesagt? (18)

How to write Ruby extensions with Crystal
How to write Ruby extensions with CrystalHow to write Ruby extensions with Crystal
How to write Ruby extensions with Crystal
 
Understanding Javascript Engines
Understanding Javascript Engines Understanding Javascript Engines
Understanding Javascript Engines
 
What's New in ES6 for Web Devs
What's New in ES6 for Web DevsWhat's New in ES6 for Web Devs
What's New in ES6 for Web Devs
 
IronSmalltalk
IronSmalltalkIronSmalltalk
IronSmalltalk
 
Denker - Pharo: Present and Future - 2009-07-14
Denker - Pharo: Present and Future - 2009-07-14Denker - Pharo: Present and Future - 2009-07-14
Denker - Pharo: Present and Future - 2009-07-14
 
JRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVMJRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVM
 
I Know Kung Fu - Juggling Java Bytecode
I Know Kung Fu - Juggling Java BytecodeI Know Kung Fu - Juggling Java Bytecode
I Know Kung Fu - Juggling Java Bytecode
 
ROP 輕鬆談
ROP 輕鬆談ROP 輕鬆談
ROP 輕鬆談
 
Implementing a JavaScript Engine
Implementing a JavaScript EngineImplementing a JavaScript Engine
Implementing a JavaScript Engine
 
Ruby Performance - The Last Mile - RubyConf India 2016
Ruby Performance - The Last Mile - RubyConf India 2016Ruby Performance - The Last Mile - RubyConf India 2016
Ruby Performance - The Last Mile - RubyConf India 2016
 
05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR matters05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR matters
 
Triton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON ChinaTriton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON China
 
Opal compiler
Opal compilerOpal compiler
Opal compiler
 
Interpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratchInterpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratch
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
 
(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings
 
JRuby 9000 - Taipei Ruby User's Group 2015
JRuby 9000 - Taipei Ruby User's Group 2015JRuby 9000 - Taipei Ruby User's Group 2015
JRuby 9000 - Taipei Ruby User's Group 2015
 
Why TypeScript?
Why TypeScript?Why TypeScript?
Why TypeScript?
 

Andere mochten auch (13)

mastercam_full
mastercam_fullmastercam_full
mastercam_full
 
INLS890_ProjectPlan
INLS890_ProjectPlanINLS890_ProjectPlan
INLS890_ProjectPlan
 
php_mysql_2006
php_mysql_2006php_mysql_2006
php_mysql_2006
 
cgipmtut
cgipmtutcgipmtut
cgipmtut
 
netbeans
netbeansnetbeans
netbeans
 
TemplateTutorial
TemplateTutorialTemplateTutorial
TemplateTutorial
 
eureka09
eureka09eureka09
eureka09
 
manual-en
manual-enmanual-en
manual-en
 
dr_1
dr_1dr_1
dr_1
 
corel-10-toc
corel-10-toccorel-10-toc
corel-10-toc
 
handout
handouthandout
handout
 
March2004-CPerlRun
March2004-CPerlRunMarch2004-CPerlRun
March2004-CPerlRun
 
User_Manual
User_ManualUser_Manual
User_Manual
 

Ähnlich wie March2004-CPerlRun

Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::Cdaoswald
 
Dr archana dhawan bajaj - csharp fundamentals slides
Dr archana dhawan bajaj - csharp fundamentals slidesDr archana dhawan bajaj - csharp fundamentals slides
Dr archana dhawan bajaj - csharp fundamentals slidesDr-archana-dhawan-bajaj
 
The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)Mike Friedman
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaPatrick Allaert
 
Basics of Javascript
Basics of JavascriptBasics of Javascript
Basics of JavascriptUniverse41
 
Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.Brian Hsu
 
An OCaml newbie meets Camlp4 parser
An OCaml newbie meets Camlp4 parserAn OCaml newbie meets Camlp4 parser
An OCaml newbie meets Camlp4 parserKiwamu Okabe
 
Debugging with pry
Debugging with pryDebugging with pry
Debugging with pryCreditas
 
(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-stringsNico Ludwig
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersKirk Kimmel
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformEastBanc Tachnologies
 
Perl 5.10
Perl 5.10Perl 5.10
Perl 5.10acme
 
Make Your Own Perl with Moops
Make Your Own Perl with MoopsMake Your Own Perl with Moops
Make Your Own Perl with MoopsMike Friedman
 
C101 – Intro to Programming with C
C101 – Intro to Programming with CC101 – Intro to Programming with C
C101 – Intro to Programming with Cgpsoft_sk
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 developmentFisnik Doko
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java ProgrammersEric Pederson
 

Ähnlich wie March2004-CPerlRun (20)

Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::C
 
Dr archana dhawan bajaj - csharp fundamentals slides
Dr archana dhawan bajaj - csharp fundamentals slidesDr archana dhawan bajaj - csharp fundamentals slides
Dr archana dhawan bajaj - csharp fundamentals slides
 
The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)
 
Chtp408
Chtp408Chtp408
Chtp408
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
Basics of Javascript
Basics of JavascriptBasics of Javascript
Basics of Javascript
 
Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.
 
An OCaml newbie meets Camlp4 parser
An OCaml newbie meets Camlp4 parserAn OCaml newbie meets Camlp4 parser
An OCaml newbie meets Camlp4 parser
 
Debugging with pry
Debugging with pryDebugging with pry
Debugging with pry
 
(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platform
 
Perl 5.10
Perl 5.10Perl 5.10
Perl 5.10
 
Karakter dan String
Karakter dan StringKarakter dan String
Karakter dan String
 
Make Your Own Perl with Moops
Make Your Own Perl with MoopsMake Your Own Perl with Moops
Make Your Own Perl with Moops
 
C101 – Intro to Programming with C
C101 – Intro to Programming with CC101 – Intro to Programming with C
C101 – Intro to Programming with C
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
 
Perl-C/C++ Integration with Swig
Perl-C/C++ Integration with SwigPerl-C/C++ Integration with Swig
Perl-C/C++ Integration with Swig
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 

Mehr von tutorialsruby

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>tutorialsruby
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheetstutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheetstutorialsruby
 

Mehr von tutorialsruby (20)

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
CSS
CSSCSS
CSS
 
CSS
CSSCSS
CSS
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 

Kürzlich hochgeladen

IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Brian Pichman
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1DianaGray10
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsSafe Software
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioChristian Posta
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 

Kürzlich hochgeladen (20)

IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and Istio
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 

March2004-CPerlRun

  • 1. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Victoria Perl Mongers (victoria.pm.org) abez@abez.ca March 16, 2004 Victoria.pm 1
  • 2. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle This Presentation • What am I going to Cover? – Inline::C – Perl Data Structures / Guts – XS Victoria.pm 2
  • 3. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle This Presentation • What am I not going to Cover? – How to program in C – C++ – Anything in depth – Magic Perl Guts – OO and Perl Guts – References and Perl Guts – SWIG (maybe you should take a look if you’re wrapping a lib) Victoria.pm 3
  • 4. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Inline::C • What is it? – Write Perl Subroutines in C [1] – Compile and Link C into Perl – Embed subroutines into perl scripts and modules – Automatically Wrap functions based on prototypes. Victoria.pm 4
  • 5. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Inline::C • How do you use Inline::C? – use Inline C => CODE; – bind Inline C => CODE; ∗ Dynamic generation of C code. – use Inline C; __END__ __C__ void cCode() { } Victoria.pm 5
  • 6. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Inline::C • How do you use Inline::C? – Inline::C parses the function prototypes and wraps them into perl. – Function prototypes should be in ANSI style ∗ return-type function-name ( type-name-pairs ) ... Victoria.pm 6
  • 7. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Inline::C • Which types are supported? – void → nothing – double → scalar – char * → string – SV* → Scalar – long → Scalar – float is not supported but you can support it Victoria.pm 7
  • 8. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Inline::C • Example use Inline C; helloWorld(); __END__ __C__ void helloWorld() { printf("Hello worldn"); } Victoria.pm 8
  • 9. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Inline::C • Example use Inline C; hello(’World’); hello(42); hello(42,42); hello(undef); __END__ __C__ void hello(char * string) { printf("Hello %sn",string); } Victoria.pm 9
  • 10. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Inline::C • Example (perl guts) use Inline C; $a = "HEY"; hello(’World’,1,42,undef,’Universe’,$a); __END__ __C__ void hello(SV* name1, ...) { Inline_Stack_Vars; int i = 0; STRLEN count; for (i = 0; i < Inline_Stack_Items; i++) { char * string = SvPV(Inline_Stack_Item(i), count); printf("Hello %sn",string); } } Victoria.pm 10
  • 11. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Inline::C • Inline Stack Access macros – Inline Stack Vars - put this macros at the top of the subroutine as it defines new various for Inline Stack macros to work on – Inline Stack Items - how many items are on the stack – Inline Stack Item(i) - get the item at index i in the stack – see perldoc Inline::C Victoria.pm 11
  • 12. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Perl Guts • Types in C [5] – SV - Scalar Value – AV - Array Value – HV - Hash Value – IV - Integer Value * – UV - Unsigned Value * – NV - Double Value * – PV - String Value * – * return values Victoria.pm 12
  • 13. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Perl Guts • Convert Scalars [5] ( these are macros ) – int SvIV(SV*) – unsigned int SvUV(SV*) – double SvNV(SV*) – char * SvPV(SV*, STRLEN len) ∗ puts length in len, i is the buffer in SV* – char * SvPV nolen(SV*) Victoria.pm 13
  • 14. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Perl Guts • Set scalars [5] ( these are macros ) – void sv setiv(SV*, IV); – void sv setuv(SV*, UV); – void sv setnv(SV*, double); – void sv setpv(SV*, const char*); – void sv setpvn(SV*, const char*, int) Victoria.pm 14
  • 15. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Perl Guts • Create scalars [5] ( these are macros ) – SV* newSViv(IV); – SV* newSVuv(UV); – SV* newSVnv(double); – SV* newSVpv(const char*, int); – SV* newSVpvn(const char*, int); – SV* newSVpvf(const char*, ...); – SV* newSVsv(SV*); Victoria.pm 15
  • 16. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Perl Guts • Arrays [5] – Make an array ∗ create - AV* newAV(); ∗ create - AV* av make(I32 num, SV **ptr); – Operations ∗ push - void av push(AV*, SV*); ∗ pop - SV* av pop(AV*); ∗ shift - SV* av shift(AV*); ∗ unshift - void av unshift(AV*, I32 num); Victoria.pm 16
  • 17. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Perl Guts • Arrays [5] – length - I32 av len(AV*); – fetch - SV** av fetch(AV*, I32 key, I32 lval); – store - SV** av store(AV*, I32 key, SV* val); – clear - void av clear(AV*); – destroy - void av undef(AV*); – expand - void av extend(AV*, I32 key); Victoria.pm 17
  • 18. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Perl Guts • Hashes – HV* newHV(); – SV** hv store(HV*, const char* key, U32 klen, SV* val, U32 hash); – SV** hv fetch(HV*, const char* key, U32 klen, I32 lval); – bool hv exists(HV*, const char* key, U32 klen); – SV* hv delete(HV*, const char* key, U32 klen, I32 flags); – void hv clear(HV*); – void hv undef(HV*); Victoria.pm 18
  • 19. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Perl Guts • Hash iteration – I32 hv iterinit(HV*); – HE* hv iternext(HV*); – char* hv iterkey(HE* entry, I32* retlen); – SV* hv iterval(HV*, HE* entry); – SV* hv iternextsv(HV*, char** key, I32* retlen); Victoria.pm 19
  • 20. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Perl Guts • Package Vars [5] – SV* get sv("package::varname", TRUE); – AV* get av("package::varname", TRUE); – HV* get hv("package::varname", TRUE); Victoria.pm 20
  • 21. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Perl Guts • Mortality – If you have temporary values such as those that are returned or used temporarily you want the garbage collection to handle it properley. – SV* sv newmortal() – SV* sv 2mortal(SV* sv) Victoria.pm 21
  • 22. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle XS • What is XS? – interface description format – Integrate Perl and C code / libs – A language that is used to integrate C w/ Perl – Wraps C calls Victoria.pm 22
  • 23. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle XS • Into the fire: [4] - Some of this is ripped from the perlxstut – h2xs -A -n PackageName – Edit PackageName.xs – perl Makefile.PL – make – Make a test script in the PackageName directory – make install – only do this if your test script has problems finding PackageName Victoria.pm 23
  • 24. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle XS • The .xs file – Prototypes are done differently, see perldoc perlxs and perlxstut – First line, return type – Second line, prototype in K&R – Following lines, type the parameters (newline is the delimiter) – int strcmp(str1,str2) char *str1 char *str2 Victoria.pm 24
  • 25. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle XS • Lets wrap a already existing function (add this to your .xs) – strcmp – #include <string.h> int strcmp(str1,str2) char *str1 char *str2 – That’s it Victoria.pm 25
  • 26. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle XS • Lets write a new function (add this to your .xs) – hello – void hello() CODE: printf("Hello Worldn"); – That’s it Victoria.pm 26
  • 27. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle XS • Lets see our test driver (after make install) #!/usr/bin/perl use Hello; Hello::hello(); print join(" ", Hello::strcmp("a","b"), Hello::strcmp("a","a"), Hello::strcmp("bbbb","aaaa") ),$/; – Victoria.pm 27
  • 28. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle Get Help • Where to get Help? – perldoc Inline – perldoc Inline::C – perldoc Inline::C-Cookbook – perldoc perlguts – perldoc perlapi – perldoc perlxs – perldoc perlxstut – perldoc perlembed Victoria.pm 28
  • 29. C Perl, C Perl Inline, C Perl XS, C Perl Guts Abram Hindle References [1] I NGERSON , B. Inline::c - write perl subroutines in c. [2] I NGERSON , B., AND WATKISS , N. Inline - write perl subroutines in other programming languages. [3] M AC E ACHERN , D., AND O RWANT, J. perlembed - how to embed perl in your c program. [4] O KAMOTO, J. perlxstut - tutorial for writing xsubs. [5] P ORTERS , P. . perlguts - introduction to the perl api. [6] R OEHRICH , D., O KAMOTO, J., AND S TUHL , B. perlapi - autogenerated documentation for the perl public api. [7] R OEHRICH , D., AND P ORTERS , T. P. perlxs - xs language reference manual. Victoria.pm 29