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:
Autoboxing test: 4+5=10 Overloading test: 4+5=9
Autoboxing doesn't cause
4
to be an instance ofoverload_me
. It causes method calls with4
on their left hand side to call methods inoverload_me
. This is a subtle distinction but an important one, because it means thatoverload
just doesn't apply at all.4
isn't an object and doesn't belong to any class. It's still just4
and when you calculate4 + 5
it's still just4 + 5
.From the autobox docs: