On Jun 3, Richard Hulse said:

>I have a module which overloads a few operators
>
>snippet:
>use overload
>     "+" => \&addoffset,
>     "-" => \&subtractoffset,
>     q("") => \&printit;
>
>Even if I allow Perl to magically make the += happen for me it still does 
>the same (wrong) thing.

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__

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
**      Manning Publications, Co, is publishing my Perl Regex book      **

Reply via email to