Skip to content

Commit 0137316

Browse files
authored
Merge pull request #4 from plicease/symbol
add Symbol attribute a la Perl6
2 parents 7701be2 + 52dc5e6 commit 0137316

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/NativeCall.pm

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ my %attr21 = (
1212
Native => 1,
1313
Args => 1,
1414
Returns => 1,
15+
Symbol => 1,
1516
);
1617

1718
sub _attr_parse {
@@ -42,7 +43,7 @@ sub MODIFY_CODE_ATTRIBUTES {
4243
}
4344
}
4445
my $subname = subname $subref;
45-
my $sub_base = (split /::/, $subname)[-1];
46+
my $sub_base = $attr2args{Symbol}->[0] // (split /::/, $subname)[-1];
4647
my $ffi = FFI::Platypus->new;
4748
my $lib = $attr2args{Native}->[0] || undef; # undef means standard library
4849
$ffi->lib($lib ? find_lib_or_die lib => $lib : undef);
@@ -79,6 +80,10 @@ NativeCall - Perl 5 interface to foreign functions in Perl code without XS
7980
8081
sub fmax :Args(double, double) :Native :Returns(double) {}
8182
say "fmax(2.0, 3.0) = " . fmax(2.0, 3.0);
83+
84+
# avoid Perl built in also called "abs"
85+
sub myabs :Args(int) :Native :Returns(int) :Symbol(abs) {}
86+
say "abs(-3) = " . abs(-3);
8287
8388
=head1 DESCRIPTION
8489
@@ -105,6 +110,10 @@ A comma-separated list of L<FFI::Platypus::Type>s.
105110
106111
A single L<FFI::Platypus::Type>.
107112
113+
=item Symbol
114+
115+
The native symbol name, if different from the Perl sub name.
116+
108117
=back
109118
110119
=head1 INSPIRATION

t/symbol.t

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use strict;
2+
use warnings;
3+
use Test::More;
4+
use FFI::Platypus;
5+
6+
my %sym;
7+
8+
no warnings 'redefine';
9+
sub FFI::Platypus::attach {
10+
my($self, $name, $args, $ret) = @_;
11+
$sym{$name->[1]} = $name->[0];
12+
$self;
13+
}
14+
15+
use parent qw( NativeCall );
16+
17+
sub foo1 :Returns(void) :Symbol(bar1) {}
18+
is $sym{'main::foo1'}, 'bar1';
19+
20+
sub foo2 :Returns(void) {}
21+
is $sym{'main::foo2'}, 'foo2';
22+
23+
done_testing;

0 commit comments

Comments
 (0)