SlideShare a Scribd company logo
1 of 29
Download to read offline
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

More Related Content

What's hot

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
 

What's hot (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?
 

Viewers also liked

Formasti
FormastiFormasti
FormastiFOMASTI
 
Find hot news in insecticides china news 1401
Find hot news in insecticides china news 1401Find hot news in insecticides china news 1401
Find hot news in insecticides china news 1401CCM Intelligence
 
Holly Rodrigue Design Samples
Holly Rodrigue Design Samples Holly Rodrigue Design Samples
Holly Rodrigue Design Samples Holly Rodrigue
 
Comunicado de Prensa Sumavisos Financiamiento
Comunicado de Prensa Sumavisos FinanciamientoComunicado de Prensa Sumavisos Financiamiento
Comunicado de Prensa Sumavisos FinanciamientoSumavisos
 
Mactac Soignies - Solutions adhésives médicales
Mactac Soignies - Solutions adhésives médicalesMactac Soignies - Solutions adhésives médicales
Mactac Soignies - Solutions adhésives médicalesMactac Europe
 
8 steps to broadcast digital signage using NoviSign
8 steps to broadcast digital signage using NoviSign8 steps to broadcast digital signage using NoviSign
8 steps to broadcast digital signage using NoviSignNoviSign
 
In network aggregation techniques for wireless sensor networks - a survey
In network aggregation techniques for wireless sensor networks - a surveyIn network aggregation techniques for wireless sensor networks - a survey
In network aggregation techniques for wireless sensor networks - a surveyGungi Achi
 
Ray white know how to consistently outperform in the uae property market
Ray white know how to consistently outperform in the uae property marketRay white know how to consistently outperform in the uae property market
Ray white know how to consistently outperform in the uae property marketJanna Yerman
 
Results 2010
Results 2010Results 2010
Results 2010matiseg
 
ESCUELA DE COMERCIO Y ADMINISTRACION.FACULTAD DE FILOSOFIA.PAULINA CHACHA.EMP...
ESCUELA DE COMERCIO Y ADMINISTRACION.FACULTAD DE FILOSOFIA.PAULINA CHACHA.EMP...ESCUELA DE COMERCIO Y ADMINISTRACION.FACULTAD DE FILOSOFIA.PAULINA CHACHA.EMP...
ESCUELA DE COMERCIO Y ADMINISTRACION.FACULTAD DE FILOSOFIA.PAULINA CHACHA.EMP...paulinap2
 

Viewers also liked (20)

Cuidadano
CuidadanoCuidadano
Cuidadano
 
Formasti
FormastiFormasti
Formasti
 
FERNANDO MENDES NOLASCO
FERNANDO MENDES NOLASCOFERNANDO MENDES NOLASCO
FERNANDO MENDES NOLASCO
 
Mg54 imagine+ events
Mg54 imagine+ eventsMg54 imagine+ events
Mg54 imagine+ events
 
Find hot news in insecticides china news 1401
Find hot news in insecticides china news 1401Find hot news in insecticides china news 1401
Find hot news in insecticides china news 1401
 
Holly Rodrigue Design Samples
Holly Rodrigue Design Samples Holly Rodrigue Design Samples
Holly Rodrigue Design Samples
 
Comunicado de Prensa Sumavisos Financiamiento
Comunicado de Prensa Sumavisos FinanciamientoComunicado de Prensa Sumavisos Financiamiento
Comunicado de Prensa Sumavisos Financiamiento
 
Mactac Soignies - Solutions adhésives médicales
Mactac Soignies - Solutions adhésives médicalesMactac Soignies - Solutions adhésives médicales
Mactac Soignies - Solutions adhésives médicales
 
8 steps to broadcast digital signage using NoviSign
8 steps to broadcast digital signage using NoviSign8 steps to broadcast digital signage using NoviSign
8 steps to broadcast digital signage using NoviSign
 
In network aggregation techniques for wireless sensor networks - a survey
In network aggregation techniques for wireless sensor networks - a surveyIn network aggregation techniques for wireless sensor networks - a survey
In network aggregation techniques for wireless sensor networks - a survey
 
Sitepal cultura de ka info
Sitepal cultura de ka infoSitepal cultura de ka info
Sitepal cultura de ka info
 
Chapter 5 (master page)
Chapter 5 (master page)Chapter 5 (master page)
Chapter 5 (master page)
 
Ray white know how to consistently outperform in the uae property market
Ray white know how to consistently outperform in the uae property marketRay white know how to consistently outperform in the uae property market
Ray white know how to consistently outperform in the uae property market
 
Anders samenwerken
Anders samenwerkenAnders samenwerken
Anders samenwerken
 
Results 2010
Results 2010Results 2010
Results 2010
 
Brand Studie 2009
Brand Studie 2009Brand Studie 2009
Brand Studie 2009
 
14 razones para_innovar
14 razones para_innovar14 razones para_innovar
14 razones para_innovar
 
Food Med Final Handbook ES
Food Med Final Handbook ESFood Med Final Handbook ES
Food Med Final Handbook ES
 
ESCUELA DE COMERCIO Y ADMINISTRACION.FACULTAD DE FILOSOFIA.PAULINA CHACHA.EMP...
ESCUELA DE COMERCIO Y ADMINISTRACION.FACULTAD DE FILOSOFIA.PAULINA CHACHA.EMP...ESCUELA DE COMERCIO Y ADMINISTRACION.FACULTAD DE FILOSOFIA.PAULINA CHACHA.EMP...
ESCUELA DE COMERCIO Y ADMINISTRACION.FACULTAD DE FILOSOFIA.PAULINA CHACHA.EMP...
 
Regelmæssige franske verber
Regelmæssige franske verberRegelmæssige franske verber
Regelmæssige franske verber
 

Similar to 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
 

Similar to 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
 

More from 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
 

More from 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
 

Recently uploaded

Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 

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