Perl autoboxing in conjunction with operator overloading doesn't seem to work?
I'm attempting to use both Perl's autoboxing functionality and operator overloading functionality, and they don't seem to be working in tandem.
Is it possible I am missing some nuance of how to use overload properly, or is this some sort of odd deviant behavior?
Sample Code:
#!/usr/bin/perl
use strict;
use warnings;
package overload_me;
use overload('+' => "overloaded_add");
sub overloaded_add{
my ($me, $him) = @_;
$me+$him+1;
}
use autobox NUMBER => 'overload_me';
my $autoboxing_test = 4->overloaded_add(5);
my $overloading_test = 4 + 5;
print "Autoboxing test: 4+5=$autoboxing_test
Overloading test: 4+5=$overloading_test\n";
Test output:
开发者_JAVA技巧Autoboxing test: 4+5=10 Overloading test: 4+5=9
Autoboxing doesn't cause 4
to be an instance of overload_me
. It causes method calls with 4
on their left hand side to call methods in overload_me
. This is a subtle distinction but an important one, because it means that overload
just doesn't apply at all. 4
isn't an object and doesn't belong to any class. It's still just 4
and when you calculate 4 + 5
it's still just 4 + 5
.
From the autobox docs:
The autoboxing is transparent: boxed values are not blessed into their (user-defined) implementation class (unless the method elects to bestow such a blessing) - they simply use its methods as though they are.
精彩评论