Have you tried taking Moose and namespace::autoclean out of the picture?

Once I remove them and fix the remaining code, it works:

#!/usr/bin/perl use 5.012003; # Perl version requirement #=========================================================== # Percentage CLASS (script below) #=========================================================== package Percentage; #use Any::Moose; #use namespace::autoclean; sub new { my $class = shift; bless { int => @_ } => $class; }; use overload q{""} => \&to_string, '0+' => \&to_num, '+' => \&add; sub to_string { my $self = shift; print "to_string()\n"; return $self->int(); } sub to_num { my $self = shift; print "to_num()\n"; return $self->int(); } sub add { my ($left, $right) = @_; print "add()\n"; my $result = new Percentage( $left->int() + $right->int() ); return $result; } sub int { $_[0]->{int} } 1; #=========================================================== # SCRIPT #=========================================================== package Example::Script; use 5.012003; # Perl version requirement use strict; use warnings; # printed in output : my $a = new Percentage(2); # build my $a_num = $a->int(); # print "$a\n"; # Percentage=Hash(0x11cfe8c) print "$a, $a_num\n"; # Percentage=Hash(0x11cfe8c), 2 my $b = new Percentage(3); # build my $c = $b + $a; # print "$b, $c\n"; # Percentage=Hash(0x116912c), 3693356 __END__ to_string() 2 to_string() 2, 2 add() to_string() to_string() 3, 5

I assume that something somewhere within Moose(::Any) or namespace::autoclean messes up the overloading. Maybe the bug reports or feature limitations tell a clearer story.

Update: Searching Moose for overload points me to Class::MOP::Class, which has an API to introspect overloading. Maybe consult that to find out whether your class actually has overloading, or maybe Moose / MOP want you to set up all your overloading through this mechanism instead of overload directly.


In reply to Re: Help! Overloading operators doesn't work by Corion
in thread [solved] Help! Overloading operators doesn't work by mascip

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.