Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: (jeffa) Re: A better mod (%) operator?

by BrowserUk (Pope)
on Jul 09, 2002 at 00:03 UTC ( #180355=note: print w/replies, xml ) Need Help??

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


    (the triplet paradiddle with high-hat)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://180355]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2021-04-23 12:39 GMT
Find Nodes?
    Voting Booth?

    No recent polls found