yogibimbi has asked for the wisdom of the Perl Monks concerning the following question:

I have some easy code which behaves a bit strangely (to my humble understanding). It is a reduced version of what I am actually running, to eliminate possible interference with other parts of the code and not take up even more space here:
use Data::Dumper; my $N = Value->new(23); warn Dumper($N * 2); warn Dumper($N*= 2); my $test; $test->{obj} = $N; warn Dumper($N * 2); warn Dumper($N*= 2); package Value; use overload ('*=' => 'mulSet'); use overload ('*' => 'mul'); sub new { my ($class, $value) = (shift, @_); $class = ref($class) || $class; my $self = {value => $value}; return bless $self, $class; } sub mul { my ($self, $num) = @_; return $self->new($self->{value} * $num); } sub mulSet { my ($self, $num) = @_; $self->{value}*= $num; return $self; }
This produces the following output:
$VAR1 = bless( { 'value' => 46 }, 'Value' ); $VAR1 = bless( { 'value' => 46 }, 'Value' ); $VAR1 = bless( { 'value' => 92 }, 'Value' ); Operation "=": no method found, argument in overloaded package Value at /bla/bla/bla/test/Value.pl line 12.
However, if I comment out either one of the following lines
$test->{obj} = $N; use overload ('*=' => 'mulSet');
or the
return $self; in mulSet, the error message does not happen. The commenting out of return $self so far is a feasible workaround to my problem, but I would like to be able to use it at some point later on and also like to know why it works, while the others produce errors, (especially what the $test->{obj} = $N; does that is so bad) , so I don't run in similar problems in the future. Workarounds never are very satisfying...

Replies are listed 'Best First'.
Re: overload conflict between * and *= (as an example)
by tobyink (Canon) on Feb 25, 2012 at 13:42 UTC

    Another workaround is:

    package Value; use overload fallback => 1;

    The explanation can be found by reading the overload documentation. Pay special attention to the section entitled Copy Constructor.

      yeah, this took care of it, thanks. I had read that part of the perldoc before, but I could not imagine how this could be relevant, but looking at the section again, lit the light for me;-)
      And any suggestion as to why the rather inconspicious and innocent $test->{obj} = $N; can screw up the entire short program? I guessed that, if Perl would consider the "=" overloaded, it had to work only on lvalues, but the offending $N here is a rvalue, so, why does this produce a reaction at all?

        The reason is that with numbers you can do this:

        my $a = 1; my $b = $a; $a++; $b++; say $a; # says "2" say $b; # says "2"

        But if $a and $b are overloaded references to numbers, then they refer to the same object, so you've actually incremented the same object twice. $a and $b are both 3.

        Perl forces you to deal with (or at least acknowledge that you've thought about) this problem by in certain circumstances forcing you to provide an overload for "=".