-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Perl examples #103
base: master
Are you sure you want to change the base?
Add Perl examples #103
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ RUN \ | |
python3.8 \ | ||
ruby2.7 \ | ||
ruby2.7-dev \ | ||
perl \ | ||
cpanminus \ | ||
\ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,7 @@ test-output/ | |
|
||
# C# | ||
*/csharp-example | ||
|
||
# Perl | ||
.perl-dependencies | ||
/local |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
requires 'FFI::Platypus', '1.00'; | ||
requires 'FFI::Platypus::Lang::Rust'; | ||
requires 'FFI::Platypus::Type::PtrObject'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use strict; | ||
use warnings; | ||
use 5.010; | ||
use FFI::Platypus 1.00; | ||
|
||
my $ffi = FFI::Platypus->new( | ||
api => 1, | ||
lang => 'Rust', | ||
); | ||
$ffi->find_lib( lib => 'integers'); | ||
$ffi->attach( addition => ['u32','u32'] => 'u32' ); | ||
|
||
say addition(1,2); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use strict; | ||
use warnings; | ||
use 5.014; | ||
use FFI::Platypus 1.00; | ||
|
||
my $ffi = FFI::Platypus->new( | ||
api => 1, | ||
lang => 'Rust', | ||
); | ||
$ffi->mangler(sub { "zip_code_database_$_[0]" }); | ||
$ffi->find_lib( lib => 'objects' ); | ||
$ffi->load_custom_type('::PtrObject' => 'ZipCodeDatabase' => 'ZipCodeDatabase' ); | ||
|
||
package ZipCodeDatabase { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Examples with a package inside of them use the package block syntax for readability. This requires Perl 5.14, which was first released in 2011. It might make sense for all of the examples to require the same version, but I've only specified versions where I need |
||
|
||
$ffi->attach( new => [] => 'opaque' => sub { | ||
my($xsub, $class) = @_; | ||
my $ptr = $xsub->(); | ||
bless { ptr => $ptr }, $class; | ||
}); | ||
|
||
$ffi->attach( populate => ['ZipCodeDatabase']); | ||
$ffi->attach( population_of => ['ZipCodeDatabase','string'] => 'u32'); | ||
|
||
$ffi->attach( free => ['opaque'] => sub { | ||
my($xsub, $self) = @_; | ||
if(my $ptr = delete $self->{ptr}) { | ||
$xsub->($ptr); | ||
} | ||
}); | ||
|
||
} | ||
|
||
my $database = ZipCodeDatabase->new; | ||
|
||
$database->populate; | ||
my $pop1 = $database->population_of("90210"); | ||
my $pop2 = $database->population_of("20500"); | ||
|
||
say $pop1 - $pop2; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use strict; | ||
use warnings; | ||
use 5.010; | ||
use FFI::Platypus 1.00; | ||
|
||
my $ffi = FFI::Platypus->new( | ||
api => 1, | ||
lang => 'Rust', | ||
); | ||
$ffi->find_lib( lib => 'slice_arguments' ); | ||
$ffi->attach( sum_of_even => [ 'u32[]', 'isize' ] => 'u32' => sub { | ||
my($xsub, $n) = @_; | ||
my $len = @$n; | ||
return $xsub->($n, $len); | ||
}); | ||
|
||
say sum_of_even([1,2,3,4,5,6]); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use strict; | ||
use warnings; | ||
use 5.010; | ||
use FFI::Platypus 1.00; | ||
|
||
my $ffi = FFI::Platypus->new( | ||
api => 1, | ||
lang => 'Rust', | ||
); | ||
$ffi->find_lib( lib => 'string_arguments' ); | ||
$ffi->attach( how_many_characters => [ 'string' ] => 'u32' ); | ||
|
||
say how_many_characters("göes to élevên"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use strict; | ||
use warnings; | ||
use 5.010; | ||
use FFI::Platypus 1.00; | ||
|
||
my $ffi = FFI::Platypus->new( | ||
api => 1, | ||
lang => 'Rust', | ||
); | ||
$ffi->find_lib( lib => 'string_return' ); | ||
|
||
$ffi->attach_cast( opaque_to_string => 'opaque' => 'string'); | ||
$ffi->attach( theme_song_free => ['opaque'] ); | ||
$ffi->attach( theme_song_generate => ['u8'] => 'opaque' => sub { | ||
my($xsub, $length) = @_; | ||
my $ptr = $xsub->($length); | ||
my $str = opaque_to_string($ptr); | ||
utf8::decode($str); | ||
theme_song_free($ptr); | ||
return $str; | ||
}); | ||
|
||
say theme_song_generate(5); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ include ${TEST_HASKELL} | |
include ${TEST_NODEJS} | ||
include ${TEST_CSHARP} | ||
include ${TEST_JULIA} | ||
include ${TEST_PERL} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
include ${COMMON_TEST_RULES} | ||
|
||
# System-wide packages need to be installed. We run the appropriate | ||
# package manager and leave a small cookie file to indicate | ||
# success. The execution depends on the cookie file. | ||
|
||
ifndef PERL_DEPENDENCIES | ||
|
||
PERL_DEPENDENCIES := .perl-dependencies | ||
|
||
${PERL_DEPENDENCIES}: cpanfile | ||
cpanm --installdeps -L local . | ||
touch $@ | ||
|
||
endif | ||
|
||
${TEST_DIR_${d}}/perl-test: LIB_DIR := ${LIB_DIR_${d}} | ||
${TEST_DIR_${d}}/perl-test: ${d}/src/main.pl ${TEST_DIR_${d}} ${LIB_${d}} ${PERL_DEPENDENCIES} | ||
LD_LIBRARY_PATH=${LIB_DIR} perl -Ilocal/lib $< > $@ | ||
|
||
.PHONY: perl-test_${d} | ||
perl-test_${d}: EXPECTED := ${d}/expected-output | ||
perl-test_${d}: ${TEST_DIR_${d}}/perl-test | ||
diff -q ${EXPECTED} $< | ||
|
||
perl: perl-test_${d} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use strict; | ||
use warnings; | ||
use 5.014; | ||
use FFI::Platypus 1.00; | ||
|
||
my $ffi = FFI::Platypus->new( | ||
api => 1, | ||
lang => 'Rust', | ||
); | ||
$ffi->find_lib( lib => 'tuples' ); | ||
|
||
package Tuple { | ||
use FFI::Platypus::Record; | ||
|
||
use overload | ||
'""' => sub { shift->to_string }, | ||
bool => sub { 1 }, fallback => 1; | ||
|
||
record_layout_1( | ||
$ffi, | ||
u32 => 'x', | ||
u32 => 'y', | ||
); | ||
|
||
sub to_string { | ||
my($self) = @_; | ||
sprintf "(%d,%d)", $self->x, $self->y; | ||
} | ||
} | ||
|
||
$ffi->attach( flip_things_around => ['record(Tuple)'] => 'record(Tuple)' ); | ||
|
||
my $tup = Tuple->new( x => 10, y => 20 ); | ||
|
||
say flip_things_around($tup); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the examples require Perl 5.10 feature of
say
which makes the code more like the other languages and easier to read without having to usprint
and explicitly put an"\n"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1