Jeff,

I have sussed it. Page 353 of the Camel book says of the += operator "the 
result is assigned to the left hand operand..."

The result of the '+ 1' method call is that a number in $self is modified. 
The last line of the method is:

$self->{_time_offset} = $offset;

...which is fine until the return from that method is assigned with += 
(this assigns $offset to whatever).

So I added:

return $self;

in the function and bingo, it works as expected!

Perl did the right thing. I just didn't realise it.

A trap for new players( like me ): Watch what your methods return when 
using assignment operators.

Duh!

Thanks for your help.

Richard


At 10:47 3/06/2001 -0400, Jeff Pinyan wrote:

>Are you doing that via:
>
>   fallback => 1
>
>in the argument list to 'use overload'?  That's how the implicit
>definitions of operators work.  It works for me:
>
>#!/usr/bin/perl -wl
>
>use overload (
>   '+' => \&add,
>   '+0' => \&num,
>   '""' => \&num,
>   fallback => 1,
>);
>
>sub add {
>   my ($l, $r, $swap) = @_;
>   ($l, $r) = ($r, $l) if $swap;
>   my $sum = (ref($l) ? $l->[0] : $l) + (ref($r) ? $r->[0] : $r);
>   return $sum;
>}
>
>sub num { $_[0][0] }
>
>$x = bless [ 10 ], 'main';
>print $x;      # 10
>print $x + 3;  # 13
>$x += 6;
>print $x;      # 16
>
>__END__

Reply via email to