Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't "use overload" work with "use namespace:autoclean"?

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?

like image 381
qodeninja Avatar asked Oct 12 '11 03:10

qodeninja


1 Answers

namespace::autoclean is futzing with the magic that overload adds to handle your operator. The following works as you would expect:

package Token;

use Moose;
use Moose::Util::TypeConstraints;

use overload '+' => '_overload_add';

has 'secretvalue' => ( is => 'rw', isa => 'Int');

sub _overload_add{
    my ( $one, $two ) = @_;
    my $value = $one->secretvalue() + $two->secretvalue();
    return ($value);
}

1;

A casual glance does not reveal anything in the namespace::autoclean docs 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 overload adds to your package. Overloading + adds the symbol table entries (), (+, and OVERLOAD. I'm guessing some or all of these are vacuumed up by namespace::autoclean, thus undoing your overloading.

If you're feeling adventurous, here's a patch for namespace::autoclean that filters out overload symbols.

like image 145
friedo Avatar answered Oct 14 '22 10:10

friedo