Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^2: Need help on hashes.

by quester (Vicar)
on Feb 04, 2008 at 04:07 UTC ( #665898=note: print w/ replies, xml ) Need Help??

in reply to Re: Need help on hashes.
in thread Need help on hashes.

Actually, no, it isn't necessary to include that if statement.

Incrementing an undef value is such a common annoyance that recent releases of perl special-case it. It seems to have been documented somewhere between version 5.004 and 5.8.8. Quoting directly from perlop, which is currently for version 5.10.0,

The auto-increment operator has a little extra builtin magic to it....

undef is always treated as numeric, and in particular is changed to 0 before incrementing (so that a post-increment of an undef value will return 0 rather than undef).

(Update:) Now that I think of it, the conversion to boolean in

if ($countries{$entries[1]}) ...
is also a special case: undef is being silently converted to false, even when using warnings and strict. If it weren't, you would have to say
if (defined ($countries{$entries[1]})) ...
which would also be fairly annoying.

Comment on Re^2: Need help on hashes.
Select or Download Code
Replies are listed 'Best First'.
Re^3: Need help on hashes.
by Errto (Vicar) on Feb 04, 2008 at 19:53 UTC

    It's funny, I've gotten so accustomed to this behavior I'd stopped thinking of it as a special case, though I think you could make an argument that it the behavior itself isn't actually, though maybe the lack of warning is.

    Let's be clear about what we mean by "recent" though. The behavior is both present and documented in Perl 5.00503 which was released in 1999.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2015-10-06 23:42 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (164 votes), past polls