Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: inheritance turns back and bites

by dragonchild (Archbishop)
on Mar 01, 2004 at 14:40 UTC ( #332926=note: print w/replies, xml ) Need Help??


in reply to inheritance turns back and bites

For the specific question, a better option might be to use overload and do dispatch based on the value you get. Something like:
package SomeData; use overload '+' => 'add', '-' => 'subtract', fallback => 1; sub add { my ($x, $y) = @_[0..1]; ($x, $y) = ($y, $x) if $_[2]; $self->{is_number} ? $x + $y : $x . $y; }

Now, personally, I would never overload + to act as string concatenation. Concatenation isn't commutative, which breaks the implicit contract most people have with addition. As well, how do you define multiplication for strings?

------
We are the carpenters and bricklayers of the Information Age.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://332926]
help
Chatterbox?
Discipulus definitevely shutted down a 12yo server

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2017-07-25 11:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (370 votes). Check out past polls.