Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Squeezing $a+1 to be magical like $a++

by jeroenes (Priest)
on Oct 10, 2001 at 14:16 UTC ( #117964=note: print w/replies, xml ) Need Help??

in reply to Squeezing $a+1 to be magical like $a++

There is Yet Another Reason why you really don't want to magicalize the adding operator, and that is the way it handles alphanumeric characters. E.g., 'z9' with ++ becomes 'aa0'. What would you want to do with 'z9' + 'a04'? That promises to get unbelievable messy. And we even haven't touched the subject of fractions... I suggest you leave the magic alone and use sprintf instead.
sub format_add{ my ($x, $y) = @_; my $ret = $x + $y; my $digs = length $x; sprintf("%0${digs}d", $ret ); }
Of course you can overload the '+' with this sub.

The ++ can do some strange things, by the way:
$a=0.5; print ++$a; #prints 1.5 $a='a0.5'; print ++$a; #prints a6 #(so ++ ignores the dot and squashes the 0) $a='2a'; print ++$a; #prints 3 #(because 2a in numeric context returns 2)

Apparently it is a beast to handle with care.

"We are not alone"(FZ)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://117964]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2018-05-27 06:08 GMT
Find Nodes?
    Voting Booth?