Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Why does $string++ work the way it does?

by Fastolfe (Vicar)
on Oct 26, 2000 at 05:51 UTC ( [id://38540]=note: print w/replies, xml ) Need Help??


in reply to Why does $string++ work the way it does?

++ is magic in that it knows how to deal with both numbers and strings. With numbers, it behaves just like a numeric increment, but since Perl is smart, it assumes that if you are ++'ing a string, that you want to change the letters around a bit.

The alternative would be to convert it to a number (0) and increment that (1), which doesn't make a lot of sense if you start off with a string, so why not make something useful out of it?

Note that this is the way that ++ differs from + and +=. The latter two operations require another argument, which can't really be anything but a number, which would convert your string to numeric form first. So if you really do want to end up with a 1 and start from a string, just use +1 or +=1 to get it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-03-28 14:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found