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...
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.