SlideShare a Scribd company logo
XS::TNG The current status of Perl-C binding ShmuelFomberg YAPC Tokyo 2010
We have XS, don’t we? XS is hard Not true. The syntax is simple XS is not hard But when writing XS you are using: XS syntax Perl guts Typesmaps C Learning all these in the same time, is hard
We are not going to talk about XS XS is a known problem A lot of documentation on the web Most of it outdated or just wrong We will talk about: Ctypes– calling C libraries from Perl Libperl++ - embedding and extending Perl using C++ library XS++  - XS – C++ binding
Who am I A Perl programmer from Israel I’m here learning Japanese Just started a three month course My first YAPC talk So, yoroshekoonegaishimasu
What is Ctypes? Port from Python’s Ctypes Good idea / interface Still in making Not on CPAN yet Enable to call C functions inside DLLs, without making XS / intermediate DLL
Ctypes Now let’s see some code: use Ctypes;print chr CDLL->msvcrt->toupper({sig=>"cii"})->(ord("y"));# prints ‘Y’ # a more verbose style:my $func = Ctypes::Function->new   ( { lib    => 'msvcrt',       name   => 'toupper',       argtypes => 'ii',       restype  => 'c' } ); print chr $func->(ord("y")); # prints ‘Y’
Ctypes Let’s see more: my $func = WinDLL->user32->                              MessageBoxA({sig=>"sipppI"}); $func->(0, "Hi", "BoxBox", 0);
Ctypes – built-in types Signature details: First char – call type. ‘s’ for stdcall, ‘c’ for cdecl. Second char – return type The rest - arguments  v =>  c_void,c =>  c_byte,C =>  c_char,s =>  c_short,S =>  c_ushort,i =>  c_int,I =>  c_uint, l =>  c_long,L =>  c_ulong,f =>  c_float,d =>  c_double,D =>  c_longdouble,p =>  c_void_p,
Building types - Simple my $array = Array( c_ushort, [ 1, 2, 3, 4, 5 ] );$array->[2] == 3;my $multi = Array( $array, $array2, $array3 );  # multidimensional  my $ushort = c_ushort(25);my $ushortp = Pointer($ushort);$$ushortp == $ushort;${$$ushortp} == 25;
Building types – Ad-hoc my $struct = Struct([  f1 => c_char('P'),  f2 => c_int(10),  f3 => c_long(90000),]); $struct->size == Ctypes::sizeof('c') + Ctypes::sizeof('i') + Ctypes::sizeof('l');$$struct->{f2} == 10; my $alignedstruct = Struct({  fields => [   o1 => c_char('Q'),   o2 => c_int(20),   o3 => c_long(180000),  ],  align => 4,});
Building types – Callback sub cb_func {  my( $ay, $bee ) = @_; return $ay <=> $bee;} my $qsort = Ctypes::Function->new  ( { lib       => 'c',        name   => 'qsort',   argtypes => 'piip‘,  restype => 'v‘,     } ); $cb = Ctypes::Callback->new( amp;cb_func, 'i', ‘ss'); my $disarray = Array(c_short , [2, 4, 5, 1, 3] );$qsort->($disarray, $#$disarray+1, Ctypes::sizeof('s'), $cb->ptr);$disarray->_update_;$disarray->[2] == 3
Ctypes – Libraries types CDLL Return type ‘i’, calling style ‘cdecl’ WinDLL Return type ‘i’, calling style ‘stdcall’ OleDLL Calling style “stdcall” Return type HRESULT Will throw exception automatically on error
Ctypes – Libraries types PerlDLL For calling Perl XS functions provides XS environment for the called function Checks the Perl error flag after the call If there was an exception – re-throws
Libperl++ Extending and Embedding Perl The goal: to let C++ do the hard work for you Reference counting Call stack operations Type conversions Exception conversions http://github.com/Leont/libperl--
Libperl++ - Embedding Let’s see some code: Interpreter universe;universe.use("Carp", 1.000); bool success = universe.eval("print qq{Hello World}");  # acessing / creating global variables  universe.scalar("status") = 404; universe.hash("table").exists("hello");  # creates @Module::data : Array data = universe.array("Module::data");
Libperl++ - Extending Let’s say we have this C++ code: class player {      string get_name() const;      pair<int, int> get_position() const;      void set_position(pair<int, int>);      double get_strength() const;      void set_strength(double);      void die(string);  };
Libperl++ - Extending It can be exported like this: Class<player> player_class = universe.add_class("Player"); player_class.add("get_name", &player::get_name); player_class.add("get_position", &player::get_position); player_class.add("set_position", &player::set_position); player_class.add("get_strength", &player::get_strength); player_class.add("set_strength", &player::set_strength); player_class.add("die", &player::die);  The first line connects a C++ class “player” with  Perl class “Player”, then we add each method
Libperl++ - Extending Of course, we can add a plain  (non-object) module Package somewhere = universe.package("foo");  somewhere.add("other_name", some_function);
Libperl++ - Extending And the Perl side: package Extend; use strict; use warnings; use Exporter 5.57 qw/import/; our @EXPORT_OK = qw/hello/; use Perlpp::Extend; 1;
Libperl++ - Calling Perl You really don’t want to know how much work is to call Perl function from C Here is how to do it with Libperl++: Ref<Code> some_function = universe.code("some_function");  some_function("Hello World"); // in scalar context some_function.list("Hello World"); // in list context
Libperl++ - Calling Perl Also works for objects: Ref<Any> testr = universe.package("Tester").call("new", 1); testr.call("print"); testr.call("set", 3); testr.call("print"); // automatically calls ‘DESTROY’
Libperl++ - Scalars Scalar value = universe.value_of(1); value += 2; // can use all normal operators: + - * / % == > < = value = "1";  // the same as setting to 1 String value = universe.value_of("test"); value == std::string("test"); // convertible to/from std::string value.replace(2, 2, value); // now contains ‘tetest’ value.insert(2, "st"); // now contains ‘testtest’ value = "test"; // simple assignment
Libperl++ - Arrays first max min shuffledsum any all none begin/end rbegin/rend push pop shift unshift reverse remove exists length clear each each_index map grep reduce Array bar = universe.list("a", "b");int length = bar.length();cout << "bla is " << baz[1] << endl; bar[4] = "e";  void test(const Scalar::Base& val); std::for_each(bar.begin(), bar.end(), test); int converter(int arg); Array singles = universe.list(1, 2, 3, 4); singles.map(converter).each(test);
Libperl++ - Hashs insert exists erase clear length each keys values Hash map = universe.hash(); map["test"] = "duck"; map["foo" ] = "bar"; void printer(const char *key,                      const Scalar::Base& value);  map.each(printer);
Libperl++ - References Any Perl variable support take_ref() Ref<Scalar> value = universe.value_of(1).take_ref(); Reference can be specific or generic Ref<Scalar>, Ref<Integer>, Ref<Number>, Ref<Any>, Ref<Hash> Reference tries to give operations shortcuts to the contained element Ref<Hash> ref = hash.take_ref(); ref["foo"] = "rab"; is_object isa is_exactly weaken bless get_classname
Libperl++ - custom type convertion structmy_type { int value; my_type(int _value) : value(_value) {} };namespace perl {    namespace typecast {        template<> structtypemap<my_type> {             static my_typecast_to(const Scalar::Base& value) {                   return my_type(value.int_value());             } typedef boost::true_typefrom_type;             static intcast_from(Interpreter&, const my_type& variable) {                  return variable.value;             }        };    } } Signals that this type can be converted to Perl type Can construct any Perl type, (using the interpreter) or any type convertible
ExtUtils::XSpp XS does not handle objects Only functions and constants You can write the object binding yourself Much fun XS++ fills the gap “transparent C++ objects in Perl”
ExtUtils::XSpp It is still XS So you need to know all the XS keywords, Perl internals and typemaps Plus adds its own little language Everything is translated back to XS for compilation Module::Build::WithXSpp See ExtUtils::XSpp’s example
XS++ - XSpp::Example.pm package XSpp::Example; use strict; use warnings;  our $VERSION = '0.01';  require XSLoader; XSLoader::load('XSpp::Example', $VERSION);  1;
XS++ - C++ headers class Animal { public:     Animal(const std::string& name);     void SetName(const std::string& newName);     std::string GetName() const;     void MakeSound() const;  private:      std::string fName; };  class Dog : public Animal {  public:      Dog(const std::string& name);      void Bark() const;      void MakeSound() const;  };
XS++ - xsp file #include "Animals.h"  %module{XSpp::Example};  class Animal {      %name{new} Animal(std::string& name);      ~Animal();      void MakeSound();      void SetName(std::string& newName);      std::string GetName();  };  class Dog : public Animal {      %name{new} Dog(std::string& name);      ~Dog();      void MakeSound();      void Bark();  };
XS++ - Dog.cc #include "Animals.h" Dog::Dog(const std::string& name) : Animal(name) {}  void Dog::Bark() const {  cout << "Woof" << endl;  }  void Dog::MakeSound() const {      Bark();  }
XS++ - Build.PL #!/usr/bin/perl -w  use strict;  use Module::Build::WithXSpp;  my $build = Module::Build::WithXSpp->new(  module_name => 'XSpp::Example',      license => 'perl',      requires => {},      # Provides extra C typemaps for opaque objects:  extra_typemap_modules => {          'ExtUtils::Typemap::ObjectMap' => '0.01',      },  ); $build->create_build_script;
XS++ - mytype.map Animal*  O_OBJECT Dog*  O_OBJECT std::string*  T_STRINGPTR  std::string  T_STRING  INPUT  T_STRING      $var = std::string(SvPV_nolen($arg))  T_STRINGPTR      $var = new std::string(SvPV_nolen($arg))  OUTPUT  T_STRING      $arg = newSVpvn($var.c_str(), $var.length());  T_STRINGPTR      $arg = newSVpvn($var->c_str(), $var->length()); Declare these types as objects to export to Perl space
More about typemaps ExtUtils::Typemap::Default And the other modules in that distribution
Thank you
Building Libffi Download from: http://github.com/atgreen/libffi Target platform: Strawberry Perl 5.12 Needed tools: cygwin  with make, without gcc To build: copy src/x86/ffitarget.h to include/ffitarget.h run bash run configure run make It will produce binaries in the “.lib” directory: Libffi.* => strewberryperl/c/lib cygffi-5.dll => strewberryperl/c/bin
Building Ctypes Download from:  http://gitorious.org/perl-ctypes http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/tpf/t127230763807 Make sure that cygwin is not available perl Makefile.pl, dmake – test – install
Building Libperl++ You need C++ Boost http://www.boost.org/ But only the headers Libperl++ is a heavy C++ templates user Using Module::Build style installer perl Build.PL perl Build perl Build test  perl Build install
Building types - Point package t_POINT;use strict;use warnings;use Ctypes;use Ctypes::Type::Struct;our @ISA = qw|Ctypes::Type::Struct|;our $_fields_ = [ ['x',c_int], ['y',c_int], ]; sub new {      my $class = ref($_[0]) || $_[0];   shift;      my $self = $class->SUPER::new({fields => $_fields_, values => [ @_ ] });      return bless $self => $class if $self;}
Building types - Point my $point = new t_POINT( 30, 40 );$$point->{x} == 30;$$point->{y} == 40;$$point->[0] == 30;$$point->[1] == 40;  # yes, it is overloaded my $point_2 = new t_POINT([ y => 30, x => 40 ]);my $point_3 = new t_POINT([ y => 50 ]);

More Related Content

What's hot

React PHP: the NodeJS challenger
React PHP: the NodeJS challengerReact PHP: the NodeJS challenger
React PHP: the NodeJS challenger
vanphp
 
Async Frontiers
Async FrontiersAsync Frontiers
Async Frontiers
Domenic Denicola
 
Asynchronous I/O in PHP
Asynchronous I/O in PHPAsynchronous I/O in PHP
Asynchronous I/O in PHPThomas Weinert
 
Modern JS with ES6
Modern JS with ES6Modern JS with ES6
Modern JS with ES6
Kevin Langley Jr.
 
ES2015 (ES6) Overview
ES2015 (ES6) OverviewES2015 (ES6) Overview
ES2015 (ES6) Overview
hesher
 
Javascript ES6 generators
Javascript ES6 generatorsJavascript ES6 generators
Javascript ES6 generators
Ramesh Nair
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Zyxware Technologies
 
Domains!
Domains!Domains!
AnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time webAnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time webclkao
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giants
Ian Barber
 
PyCon lightning talk on my Toro module for Tornado
PyCon lightning talk on my Toro module for TornadoPyCon lightning talk on my Toro module for Tornado
PyCon lightning talk on my Toro module for Tornado
emptysquare
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6Solution4Future
 
ES6 generators
ES6 generatorsES6 generators
ES6 generators
Steven Foote
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
Ian Barber
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is here
Sebastiano Armeli
 
ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 Version
Ian Barber
 
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
Rami Sayar
 
Trading with opensource tools, two years later
Trading with opensource tools, two years laterTrading with opensource tools, two years later
Trading with opensource tools, two years laterclkao
 
node ffi
node ffinode ffi
node ffi
偉格 高
 
Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6
Nilesh Jayanandana
 

What's hot (20)

React PHP: the NodeJS challenger
React PHP: the NodeJS challengerReact PHP: the NodeJS challenger
React PHP: the NodeJS challenger
 
Async Frontiers
Async FrontiersAsync Frontiers
Async Frontiers
 
Asynchronous I/O in PHP
Asynchronous I/O in PHPAsynchronous I/O in PHP
Asynchronous I/O in PHP
 
Modern JS with ES6
Modern JS with ES6Modern JS with ES6
Modern JS with ES6
 
ES2015 (ES6) Overview
ES2015 (ES6) OverviewES2015 (ES6) Overview
ES2015 (ES6) Overview
 
Javascript ES6 generators
Javascript ES6 generatorsJavascript ES6 generators
Javascript ES6 generators
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
 
Domains!
Domains!Domains!
Domains!
 
AnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time webAnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time web
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giants
 
PyCon lightning talk on my Toro module for Tornado
PyCon lightning talk on my Toro module for TornadoPyCon lightning talk on my Toro module for Tornado
PyCon lightning talk on my Toro module for Tornado
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6
 
ES6 generators
ES6 generatorsES6 generators
ES6 generators
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is here
 
ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 Version
 
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
 
Trading with opensource tools, two years later
Trading with opensource tools, two years laterTrading with opensource tools, two years later
Trading with opensource tools, two years later
 
node ffi
node ffinode ffi
node ffi
 
Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6
 

Viewers also liked

CPAN Module Maintenance
CPAN Module MaintenanceCPAN Module Maintenance
CPAN Module Maintenance
Dave Cross
 
Introduction to Modern Perl
Introduction to Modern PerlIntroduction to Modern Perl
Introduction to Modern Perl
Dave Cross
 
Intermediate Perl
Intermediate PerlIntermediate Perl
Intermediate Perl
Dave Cross
 
Semantic Pipes (London Perl Workshop 2009)
Semantic Pipes (London Perl Workshop 2009)Semantic Pipes (London Perl Workshop 2009)
Semantic Pipes (London Perl Workshop 2009)
osfameron
 
C:\Fakepath\Slideshare
C:\Fakepath\SlideshareC:\Fakepath\Slideshare
C:\Fakepath\Slidesharemurcelly
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional Programmer
Dave Cross
 
Sunrays
SunraysSunrays
Sunrays
Vili 48
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Dave Cross
 
Savings account
Savings accountSavings account
Savings account
Madhura Joshi
 
Current account
Current accountCurrent account
Current account
Arvind Shrivastav
 
Current account deficit of india
Current account deficit of indiaCurrent account deficit of india
Current account deficit of india
Sunanda Sarker
 
Saving account
Saving account Saving account
Saving account
Arvind Shrivastav
 
Current Account
Current AccountCurrent Account
Importance Of Banks In An Economy
Importance Of Banks In An EconomyImportance Of Banks In An Economy
Importance Of Banks In An Economy
Rudo Chengeta
 
Saving Account
Saving AccountSaving Account
14 Principles of HENRI FAYOL project on KFC Class-XII
14 Principles of HENRI FAYOL  project on KFC Class-XII14 Principles of HENRI FAYOL  project on KFC Class-XII
14 Principles of HENRI FAYOL project on KFC Class-XII
Atif Khan
 

Viewers also liked (16)

CPAN Module Maintenance
CPAN Module MaintenanceCPAN Module Maintenance
CPAN Module Maintenance
 
Introduction to Modern Perl
Introduction to Modern PerlIntroduction to Modern Perl
Introduction to Modern Perl
 
Intermediate Perl
Intermediate PerlIntermediate Perl
Intermediate Perl
 
Semantic Pipes (London Perl Workshop 2009)
Semantic Pipes (London Perl Workshop 2009)Semantic Pipes (London Perl Workshop 2009)
Semantic Pipes (London Perl Workshop 2009)
 
C:\Fakepath\Slideshare
C:\Fakepath\SlideshareC:\Fakepath\Slideshare
C:\Fakepath\Slideshare
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional Programmer
 
Sunrays
SunraysSunrays
Sunrays
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Savings account
Savings accountSavings account
Savings account
 
Current account
Current accountCurrent account
Current account
 
Current account deficit of india
Current account deficit of indiaCurrent account deficit of india
Current account deficit of india
 
Saving account
Saving account Saving account
Saving account
 
Current Account
Current AccountCurrent Account
Current Account
 
Importance Of Banks In An Economy
Importance Of Banks In An EconomyImportance Of Banks In An Economy
Importance Of Banks In An Economy
 
Saving Account
Saving AccountSaving Account
Saving Account
 
14 Principles of HENRI FAYOL project on KFC Class-XII
14 Principles of HENRI FAYOL  project on KFC Class-XII14 Principles of HENRI FAYOL  project on KFC Class-XII
14 Principles of HENRI FAYOL project on KFC Class-XII
 

Similar to C to perl binding

How Xslate Works
How Xslate WorksHow Xslate Works
How Xslate Works
Goro Fuji
 
Fantom and Tales
Fantom and TalesFantom and Tales
Fantom and Tales
kaushik_sathupadi
 
Embed--Basic PERL XS
Embed--Basic PERL XSEmbed--Basic PERL XS
Embed--Basic PERL XS
byterock
 
Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3
Peter Maas
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
Sopan Shewale
 
Python - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave ParkPython - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave Park
pointstechgeeks
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
Bartlomiej Filipek
 
Groovy
GroovyGroovy
Groovy
Zen Urban
 
Scala 3camp 2011
Scala   3camp 2011Scala   3camp 2011
Scala 3camp 2011
Scalac
 
Perl Moderno
Perl ModernoPerl Moderno
Perl Moderno
Tiago Peczenyj
 
Modern Perl
Modern PerlModern Perl
Modern Perl
Marcos Rebelo
 
Design Patterns in Ruby
Design Patterns in RubyDesign Patterns in Ruby
Design Patterns in Ruby
Aleksander Dąbrowski
 
Einführung in TypeScript
Einführung in TypeScriptEinführung in TypeScript
Einführung in TypeScript
Demian Holderegger
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008
Guillaume Laforge
 
Python scripting kick off
Python scripting kick offPython scripting kick off
Python scripting kick off
Andrea Gangemi
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
climatewarrior
 
Introduction To Groovy 2005
Introduction To Groovy 2005Introduction To Groovy 2005
Introduction To Groovy 2005
Tugdual Grall
 

Similar to C to perl binding (20)

How Xslate Works
How Xslate WorksHow Xslate Works
How Xslate Works
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
Fantom and Tales
Fantom and TalesFantom and Tales
Fantom and Tales
 
Embed--Basic PERL XS
Embed--Basic PERL XSEmbed--Basic PERL XS
Embed--Basic PERL XS
 
Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3
 
Antlr V3
Antlr V3Antlr V3
Antlr V3
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Python - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave ParkPython - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave Park
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
Groovy
GroovyGroovy
Groovy
 
Scala 3camp 2011
Scala   3camp 2011Scala   3camp 2011
Scala 3camp 2011
 
Perl Moderno
Perl ModernoPerl Moderno
Perl Moderno
 
Modern Perl
Modern PerlModern Perl
Modern Perl
 
Design Patterns in Ruby
Design Patterns in RubyDesign Patterns in Ruby
Design Patterns in Ruby
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Einführung in TypeScript
Einführung in TypeScriptEinführung in TypeScript
Einführung in TypeScript
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008
 
Python scripting kick off
Python scripting kick offPython scripting kick off
Python scripting kick off
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
Introduction To Groovy 2005
Introduction To Groovy 2005Introduction To Groovy 2005
Introduction To Groovy 2005
 

Recently uploaded

Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 

Recently uploaded (20)

Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 

C to perl binding

  • 1. XS::TNG The current status of Perl-C binding ShmuelFomberg YAPC Tokyo 2010
  • 2. We have XS, don’t we? XS is hard Not true. The syntax is simple XS is not hard But when writing XS you are using: XS syntax Perl guts Typesmaps C Learning all these in the same time, is hard
  • 3. We are not going to talk about XS XS is a known problem A lot of documentation on the web Most of it outdated or just wrong We will talk about: Ctypes– calling C libraries from Perl Libperl++ - embedding and extending Perl using C++ library XS++ - XS – C++ binding
  • 4. Who am I A Perl programmer from Israel I’m here learning Japanese Just started a three month course My first YAPC talk So, yoroshekoonegaishimasu
  • 5. What is Ctypes? Port from Python’s Ctypes Good idea / interface Still in making Not on CPAN yet Enable to call C functions inside DLLs, without making XS / intermediate DLL
  • 6. Ctypes Now let’s see some code: use Ctypes;print chr CDLL->msvcrt->toupper({sig=>"cii"})->(ord("y"));# prints ‘Y’ # a more verbose style:my $func = Ctypes::Function->new ( { lib => 'msvcrt', name => 'toupper', argtypes => 'ii', restype => 'c' } ); print chr $func->(ord("y")); # prints ‘Y’
  • 7. Ctypes Let’s see more: my $func = WinDLL->user32-> MessageBoxA({sig=>"sipppI"}); $func->(0, "Hi", "BoxBox", 0);
  • 8. Ctypes – built-in types Signature details: First char – call type. ‘s’ for stdcall, ‘c’ for cdecl. Second char – return type The rest - arguments v => c_void,c => c_byte,C => c_char,s => c_short,S => c_ushort,i => c_int,I => c_uint, l => c_long,L => c_ulong,f => c_float,d => c_double,D => c_longdouble,p => c_void_p,
  • 9. Building types - Simple my $array = Array( c_ushort, [ 1, 2, 3, 4, 5 ] );$array->[2] == 3;my $multi = Array( $array, $array2, $array3 ); # multidimensional my $ushort = c_ushort(25);my $ushortp = Pointer($ushort);$$ushortp == $ushort;${$$ushortp} == 25;
  • 10. Building types – Ad-hoc my $struct = Struct([  f1 => c_char('P'),  f2 => c_int(10),  f3 => c_long(90000),]); $struct->size == Ctypes::sizeof('c') + Ctypes::sizeof('i') + Ctypes::sizeof('l');$$struct->{f2} == 10; my $alignedstruct = Struct({  fields => [   o1 => c_char('Q'),   o2 => c_int(20),   o3 => c_long(180000),  ],  align => 4,});
  • 11. Building types – Callback sub cb_func {  my( $ay, $bee ) = @_; return $ay <=> $bee;} my $qsort = Ctypes::Function->new  ( { lib    => 'c', name   => 'qsort',   argtypes => 'piip‘, restype => 'v‘, } ); $cb = Ctypes::Callback->new( amp;cb_func, 'i', ‘ss'); my $disarray = Array(c_short , [2, 4, 5, 1, 3] );$qsort->($disarray, $#$disarray+1, Ctypes::sizeof('s'), $cb->ptr);$disarray->_update_;$disarray->[2] == 3
  • 12. Ctypes – Libraries types CDLL Return type ‘i’, calling style ‘cdecl’ WinDLL Return type ‘i’, calling style ‘stdcall’ OleDLL Calling style “stdcall” Return type HRESULT Will throw exception automatically on error
  • 13. Ctypes – Libraries types PerlDLL For calling Perl XS functions provides XS environment for the called function Checks the Perl error flag after the call If there was an exception – re-throws
  • 14. Libperl++ Extending and Embedding Perl The goal: to let C++ do the hard work for you Reference counting Call stack operations Type conversions Exception conversions http://github.com/Leont/libperl--
  • 15. Libperl++ - Embedding Let’s see some code: Interpreter universe;universe.use("Carp", 1.000); bool success = universe.eval("print qq{Hello World}"); # acessing / creating global variables universe.scalar("status") = 404; universe.hash("table").exists("hello"); # creates @Module::data : Array data = universe.array("Module::data");
  • 16. Libperl++ - Extending Let’s say we have this C++ code: class player { string get_name() const; pair<int, int> get_position() const; void set_position(pair<int, int>); double get_strength() const; void set_strength(double); void die(string); };
  • 17. Libperl++ - Extending It can be exported like this: Class<player> player_class = universe.add_class("Player"); player_class.add("get_name", &player::get_name); player_class.add("get_position", &player::get_position); player_class.add("set_position", &player::set_position); player_class.add("get_strength", &player::get_strength); player_class.add("set_strength", &player::set_strength); player_class.add("die", &player::die); The first line connects a C++ class “player” with Perl class “Player”, then we add each method
  • 18. Libperl++ - Extending Of course, we can add a plain (non-object) module Package somewhere = universe.package("foo"); somewhere.add("other_name", some_function);
  • 19. Libperl++ - Extending And the Perl side: package Extend; use strict; use warnings; use Exporter 5.57 qw/import/; our @EXPORT_OK = qw/hello/; use Perlpp::Extend; 1;
  • 20. Libperl++ - Calling Perl You really don’t want to know how much work is to call Perl function from C Here is how to do it with Libperl++: Ref<Code> some_function = universe.code("some_function"); some_function("Hello World"); // in scalar context some_function.list("Hello World"); // in list context
  • 21. Libperl++ - Calling Perl Also works for objects: Ref<Any> testr = universe.package("Tester").call("new", 1); testr.call("print"); testr.call("set", 3); testr.call("print"); // automatically calls ‘DESTROY’
  • 22. Libperl++ - Scalars Scalar value = universe.value_of(1); value += 2; // can use all normal operators: + - * / % == > < = value = "1"; // the same as setting to 1 String value = universe.value_of("test"); value == std::string("test"); // convertible to/from std::string value.replace(2, 2, value); // now contains ‘tetest’ value.insert(2, "st"); // now contains ‘testtest’ value = "test"; // simple assignment
  • 23. Libperl++ - Arrays first max min shuffledsum any all none begin/end rbegin/rend push pop shift unshift reverse remove exists length clear each each_index map grep reduce Array bar = universe.list("a", "b");int length = bar.length();cout << "bla is " << baz[1] << endl; bar[4] = "e"; void test(const Scalar::Base& val); std::for_each(bar.begin(), bar.end(), test); int converter(int arg); Array singles = universe.list(1, 2, 3, 4); singles.map(converter).each(test);
  • 24. Libperl++ - Hashs insert exists erase clear length each keys values Hash map = universe.hash(); map["test"] = "duck"; map["foo" ] = "bar"; void printer(const char *key, const Scalar::Base& value); map.each(printer);
  • 25. Libperl++ - References Any Perl variable support take_ref() Ref<Scalar> value = universe.value_of(1).take_ref(); Reference can be specific or generic Ref<Scalar>, Ref<Integer>, Ref<Number>, Ref<Any>, Ref<Hash> Reference tries to give operations shortcuts to the contained element Ref<Hash> ref = hash.take_ref(); ref["foo"] = "rab"; is_object isa is_exactly weaken bless get_classname
  • 26. Libperl++ - custom type convertion structmy_type { int value; my_type(int _value) : value(_value) {} };namespace perl { namespace typecast { template<> structtypemap<my_type> { static my_typecast_to(const Scalar::Base& value) { return my_type(value.int_value()); } typedef boost::true_typefrom_type; static intcast_from(Interpreter&, const my_type& variable) { return variable.value; } }; } } Signals that this type can be converted to Perl type Can construct any Perl type, (using the interpreter) or any type convertible
  • 27. ExtUtils::XSpp XS does not handle objects Only functions and constants You can write the object binding yourself Much fun XS++ fills the gap “transparent C++ objects in Perl”
  • 28. ExtUtils::XSpp It is still XS So you need to know all the XS keywords, Perl internals and typemaps Plus adds its own little language Everything is translated back to XS for compilation Module::Build::WithXSpp See ExtUtils::XSpp’s example
  • 29. XS++ - XSpp::Example.pm package XSpp::Example; use strict; use warnings; our $VERSION = '0.01'; require XSLoader; XSLoader::load('XSpp::Example', $VERSION); 1;
  • 30. XS++ - C++ headers class Animal { public: Animal(const std::string& name); void SetName(const std::string& newName); std::string GetName() const; void MakeSound() const; private: std::string fName; }; class Dog : public Animal { public: Dog(const std::string& name); void Bark() const; void MakeSound() const; };
  • 31. XS++ - xsp file #include "Animals.h" %module{XSpp::Example}; class Animal { %name{new} Animal(std::string& name); ~Animal(); void MakeSound(); void SetName(std::string& newName); std::string GetName(); }; class Dog : public Animal { %name{new} Dog(std::string& name); ~Dog(); void MakeSound(); void Bark(); };
  • 32. XS++ - Dog.cc #include "Animals.h" Dog::Dog(const std::string& name) : Animal(name) {} void Dog::Bark() const { cout << "Woof" << endl; } void Dog::MakeSound() const { Bark(); }
  • 33. XS++ - Build.PL #!/usr/bin/perl -w use strict; use Module::Build::WithXSpp; my $build = Module::Build::WithXSpp->new( module_name => 'XSpp::Example', license => 'perl', requires => {}, # Provides extra C typemaps for opaque objects: extra_typemap_modules => { 'ExtUtils::Typemap::ObjectMap' => '0.01', }, ); $build->create_build_script;
  • 34. XS++ - mytype.map Animal* O_OBJECT Dog* O_OBJECT std::string* T_STRINGPTR std::string T_STRING INPUT T_STRING $var = std::string(SvPV_nolen($arg)) T_STRINGPTR $var = new std::string(SvPV_nolen($arg)) OUTPUT T_STRING $arg = newSVpvn($var.c_str(), $var.length()); T_STRINGPTR $arg = newSVpvn($var->c_str(), $var->length()); Declare these types as objects to export to Perl space
  • 35. More about typemaps ExtUtils::Typemap::Default And the other modules in that distribution
  • 37. Building Libffi Download from: http://github.com/atgreen/libffi Target platform: Strawberry Perl 5.12 Needed tools: cygwin with make, without gcc To build: copy src/x86/ffitarget.h to include/ffitarget.h run bash run configure run make It will produce binaries in the “.lib” directory: Libffi.* => strewberryperl/c/lib cygffi-5.dll => strewberryperl/c/bin
  • 38. Building Ctypes Download from: http://gitorious.org/perl-ctypes http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/tpf/t127230763807 Make sure that cygwin is not available perl Makefile.pl, dmake – test – install
  • 39. Building Libperl++ You need C++ Boost http://www.boost.org/ But only the headers Libperl++ is a heavy C++ templates user Using Module::Build style installer perl Build.PL perl Build perl Build test perl Build install
  • 40. Building types - Point package t_POINT;use strict;use warnings;use Ctypes;use Ctypes::Type::Struct;our @ISA = qw|Ctypes::Type::Struct|;our $_fields_ = [ ['x',c_int], ['y',c_int], ]; sub new { my $class = ref($_[0]) || $_[0]; shift; my $self = $class->SUPER::new({fields => $_fields_, values => [ @_ ] }); return bless $self => $class if $self;}
  • 41. Building types - Point my $point = new t_POINT( 30, 40 );$$point->{x} == 30;$$point->{y} == 40;$$point->[0] == 30;$$point->[1] == 40; # yes, it is overloaded my $point_2 = new t_POINT([ y => 30, x => 40 ]);my $point_3 = new t_POINT([ y => 50 ]);