I actually don't fully understand the code you've provided, but I'll certainly try to remedy that when I get the chance.
First up, I wanted to remove the "experimental" requirement - assuming it's not a crucial piece of the demo. (I'm not at all familiar with experimental stuff, and I don't have much interest in it.)
I've also added some dumps of some more subtractions.
So, I've arrived at this modified version of choroba's script :
use warnings;
use strict;
#use experimental qw( signatures );
{ package My::A;
use overload '-' => \−
# sub minus($x, $y, $swap) { ############ Replaced by next line ##
sub minus { my($x, $y, $swap) = (shift, shift, shift);
return ref($x)->new(
($y->isa('My::B') ? $x->[0] - $y->{value}
: ($x->[0] - $y->[0])
) * (1, -1)[$swap]
)
}
# sub new($class, $value) { ############ Replaced by next line ##
sub new { my($class, $value) = (shift, shift);
bless [$value], $class
}
# We can change My::B outside of B, in fact.
eval q{ package My::B;
my $m = My::B->can('minus') or die 'My::B not loaded';
use overload "-" => sub { my($x, $y, $s) = (shift, shift,
+ shift);
return $y->isa('My::A') ? $y->minus($x, 1) : $m->($x,
+$y, $s)
}
};
}
{ package My::B; # Can't be changed!
use overload '-' => \−
# sub minus($x, $y, $swap) { ############ Replaced by next line ##
sub minus { my($x, $y, $swap) = (shift, shift, shift);
my $subtr = $x->{value} - $y->{value};
return ref($x)->new($swap ? -$subtr : $subtr)
}
# sub new($class, $value) { ############ Replaced by next line ##
sub new { my($class, $value) = (shift, shift);
bless {value => $value}, $class
}
}
my $a0 = 'My::A'->new(16);
my $a1 = 'My::A'->new(6);
my $b0 = 'My::B'->new(16);
my $b1 = 'My::B'->new(6);
use Data::Dumper;
print Dumper A => $a0 - $a1;
print Dumper B => $a0 - $b1;
print Dumper C => $b0 - $b1;
print Dumper D => $b0 - $a1;
print Dumper AA => $a1 - $a0;
print Dumper BB => $b1 - $a0;
print Dumper CC => $b1 - $b0;
print Dumper DD => $a1 - $b0;
At this point, I have 2 questions:
1. Where is can documented ?
2. How is it that I can use isa even though no features have been enabled ?
Regarding the first question:
>perldoc -f can
No documentation for perl function 'can' found
WRT the second question, the perlop documentation says this regarding isa:
This feature is available from Perl 5.31.6 onwards when enabled by
+ "use
feature 'isa'". This feature is enabled automatically by a "use v5
+.36"
(or higher) declaration in the current scope.
I have done neither of those things. Has perl-5.42.0 automatically called that feature for me ? ... or does it automatically call "use v5.36" ?
Cheers, Rob | [reply] [d/l] [select] |
| [reply] [d/l] [select] |
Thanks for clarifying that Athanasius.
I've added some debug print() commands to help me understand the flow, and I've altered the code that performs (and displays the results of) the subtractions.
Here 'tis:
use warnings;
use strict;
{ package My::A;
use overload '-' => \−
sub minus { my($x, $y, $swap) = (shift, shift, shift);
print "In My::A minus\n";
return ref($x)->new(
($y->isa('My::B') ? $x->[0] - $y->{value}
: ($x->[0] - $y->[0])
) * (1, -1)[$swap]
)
}
sub new { my($class, $value) = (shift, shift);
bless [$value], $class
}
# We can change My::B outside of My::B, in fact.
eval q| package My::B;
print "Running the eval in My::A\n";
my $m = My::B->can('minus') or die 'My::B not loaded';# $m
+ is a reference to My::B::minus().
use overload "-" => sub { my($x, $y, $s) = (shift, shift,
+ shift);
print "From overload sub in My::A eval\n";
return $y->isa('My::A') ? $y->minus($x, 1) : $m->($x, $
+y, $s)
}
|;
}
{ package My::B; # Can't be changed!
use overload '-' => \−
sub minus { my($x, $y, $swap) = (shift, shift, shift);
print "In My::B::minus\n";
my $subtr = $x->{value} - $y->{value};
return ref($x)->new($swap ? -$subtr : $subtr)
}
sub new { my($class, $value) = (shift, shift);
bless {value => $value}, $class
}
}
my $a0 = My::A->new(16);
my $a1 = My::A->new(6);
my $b0 = My::B->new(16);
my $b1 = My::B->new(6);
print "All values assigned\n";
my $r1 = $a0 - $a1;
print ref($r1), ' = ', ref($a0), ' - ', ref($a1), "\n\n";
my $r2 = $a0 - $b1;
print ref($r2), ' = ', ref($a0), ' - ', ref($b1), "\n\n";
my $r3 = $b1 - $a0;
print ref($r3), ' = ', ref($b1), ' - ', ref($a0), "\n\n";
my $r4 = $b0 - $b1;
print ref($r4), ' = ', ref($b0), ' - ', ref($b1), "\n\n";
__END__
Outputs:
Running the eval in My::A
All values assigned
In My::A minus
My::A = My::A - My::A
In My::A minus
My::A = My::A - My::B
From overload sub in My::A eval
In My::A minus
My::A = My::B - My::A
From overload sub in My::A eval
In My::B::minus
My::B = My::B - My::B
I gather that:
1) the eval in My::A gets executed at start-up and sets its own subtraction subroutine as the subtraction subroutine that My::B's overloading of '-' will call (instead of calling My::B::minus);
2) it's the same behaviour, irrespective of the order in which packages My::A and My::B are loaded;
3) it would be the same behaviour if the 2 packages were in separate files (My/A.pm and My/B.pm), irrespective of the order in which the 2 pm files were loaded.
I certainly didn't know that this was do-able - and thanks, choroba, for drawing my attention to it.
It's something I can probably utilize wrt the overloading of objects from modules that I don't maintain.
Are there any caveats regarding the process by which this works ?
Cheers, Rob | [reply] [d/l] |