Permalink
Browse files

Support overloading numification of Perl 5 objects

  • Loading branch information...
niner committed Oct 2, 2017
1 parent 92fe4b5 commit d7d908fcc7c1d9131b2018adfe287ccc25b84b6e
Showing with 14 additions and 1 deletion.
  1. +5 −0 lib/Inline/Perl5/Object.pm6
  2. +9 −1 t/overload.t
@@ -19,6 +19,11 @@ class Inline::Perl5::Object {
return $stringify ?? $stringify(self) !! callsame;
}
method Numeric() {
my $numify = $!perl5.call('overload::Method', self, '0+');
return $numify ?? $numify(self) !! callsame;
}
submethod DESTROY {
$!perl5.sv_refcnt_dec($!ptr) if $!ptr;
$!ptr = Pointer;
View
@@ -8,10 +8,16 @@ my $p5 = Inline::Perl5.new;
$p5.run: q:heredoc/PERL5/;
package Foo;
use overload '""' => sub {
use overload
'""' => sub {
my ($self) = @_;
return $$self;
},
"0+" => sub {
my ($self) = @_;
return 42;
};
sub new {
@@ -29,10 +35,12 @@ $p5.run: q:heredoc/PERL5/;
my $foo = $p5.invoke('Foo', 'new', 'a string!');
is("$foo", 'a string!');
unlike("$foo", /"Inline::Perl5::Object"\<\d+\>/);
is(+$foo, 42);
my $bar = $p5.invoke('Bar', 'new', 'a string!');
isnt("$bar", 'a string!');
like("$bar", /"Inline::Perl5::Object"\<\-?\d+\>/);
isnt((try +$bar) // 0, 42);
done-testing;

0 comments on commit d7d908f

Please sign in to comment.