Ok just to sanity check overload doesnt seem to be working for me. I don’t know if it’s the version of perl I have, or the version of overload.pm, or something wrong with how I’ve implemented it, but this code doesnt work for me.
perl version
This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi
overload version
perl -Moverload -e 'print "$overload::VERSION\n";'
1.07
Token.pm
package Token;
use namespace::autoclean;
use Data::Dumper;
use Moose;
use Moose::Util::TypeConstraints;
use overload '+' => \&_overload_add, fallback => 1;
has 'secretvalue' => ( is => 'rw', isa => 'Int');
sub _overload_add{
my ( $one, $two ) = @_;
my $value = $one->secretvalue() + $two->secretvalue();
return ($value);
}
main
use strict;
use warnings;
use Token;
my $t = Token->new( secretvalue => 17, key => 'x' );
my $t2 = Token->new( secretvalue => 12, key => 'y' );
my $x = $t + $t2;
print $x;
prints
$VAR1 = 12900840;
The worst part is that I’m not getting any kind of warning or errors in the log.
UPDATE
Thanks to Freido for finding the problem. I’ve updated the question just in case anyone else stumbles on this.
Does the Perl/Moose community generally not use overload?
namespace::autocleanis futzing with the magic thatoverloadadds to handle your operator. The following works as you would expect:A casual glance does not reveal anything in the
namespace::autocleandocs that addresses this, so I guess it’s an unanticipated feature. Some searching reveals that a bug has been reported.I guess it comes down the special symbols that
overloadadds to your package. Overloading+adds the symbol table entries(),(+, andOVERLOAD. I’m guessing some or all of these are vacuumed up bynamespace::autoclean, thus undoing your overloading.If you’re feeling adventurous, here’s a patch for
namespace::autocleanthat filters out overload symbols.