Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

[solved] Help! Overloading operators doesn't work

by mascip (Pilgrim)
on Sep 01, 2012 at 11:14 UTC ( #991143=perlquestion: print w/ replies, xml ) Need Help??
mascip has asked for the wisdom of the Perl Monks concerning the following question:

* Edit : Solved. It was because of namespace::autoclean => i'll use namespace::sweep instead.

None of the operators overloading work in this simple example. What did i do wrong? Probably a simple stupid mistake...
Here is the code (Percent class, followed by very simple script), and the printed output is in comments.

#!/usr/bin/perl use 5.012003; # Perl version requirement #=========================================================== # Percentage CLASS (script below) #=========================================================== package Percentage; use Any::Moose; use namespace::autoclean; has 'int' => ( is => 'ro', isa => 'Int', ); around BUILDARGS => sub { my $orig = shift; my $class = shift; print "build\n"; return $class->$orig(int => @_); }; 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; } __PACKAGE__->meta->make_immutable; 1; #=========================================================== # SCRIPT #=========================================================== package Example::Script; use 5.012003; # Perl version requirement use strict; use warnings; # printed in output : my $a = new Percentage(2); # build print "$a\n"; # Percentage=Hash(0x11cfe8c) my $a_num = $a->int(); # my $a_str = "$a"; # print "$a_str, $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

Comment on [solved] Help! Overloading operators doesn't work
Download Code
Replies are listed 'Best First'.
Re: Help! Overloading operators doesn't work
by Corion (Pope) on Sep 01, 2012 at 11:30 UTC

    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.

      You're right, it's because of namespace::autoclean

      There is an example in the Moose Cookbook, of overloading with Moose : http://search.cpan.org/~stevan/Moose/lib/Moose/Cookbook/Basics/Recipe9.pod
      It uses the overloading operator.

      According to a namespace::autoclean review, if you use overloading with Moose you should use namespace::sweep instead of namespace::autoclean : "This pragma was written to address some problems with the excellent namespace::autoclean. In particular, namespace::autoclean will remove special symbols that are installed by overload, so you can't use namespace::autoclean on objects that overload Perl operators."

Re: Help! Overloading operators doesn't work
by philiprbrenan (Monk) on Sep 01, 2012 at 11:37 UTC

    Please tell me where $a_str is declared before it is used in:

    print "$a_str, $a_num\n";

    I cannot get your script to compile without it. Thanks.

      Added, my mistake.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991143]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2015-07-31 03:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls