http://qs321.pair.com?node_id=180355


in reply to (jeffa) Re: A better mod (%) operator?
in thread A better mod (%) operator?

Thanks Jeffa.

It looks as if you are being bitten by an "undocumented feature" of wantarray(). Rest assured though your efforts were not wasted, this small sample has assisted my education at least.

With regard to the code. If I understand what I am seeing, the overloaded operator will only be called if the variables to which its applied have been blessed into the package in which the overloading is done by the use of new()?

I assume from this that there is no way to overload an operator such that it will operate without requiring this?

  • Comment on Re: (jeffa) Re: A better mod (%) operator?

Replies are listed 'Best First'.
(jeffa) 3Re: A better mod (%) operator?
by jeffa (Bishop) on Jul 09, 2002 at 02:18 UTC
    Yep, you have to have a blessed package (an object) in order for overload to work - but who says you can't bless main? ;)
    use strict; use overload '+' => sub { ${$_[0]} - ${$_[1]} }; use overload '-' => sub { ${$_[0]} + ${$_[1]} }; my ($x,$y) = (90,60); my $i = bless \$x, 'main'; my $j = bless \$y, 'main'; # objects use overloaded operators print $i + $j, $/; # 30 print $i - $j, $/; # 150 # normal scalars use Perl operators print $x + $y, $/; # 150 print $x - $y, $/; # 30

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)