Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Making a failed hash lookup return something other than undef

by thelenm (Vicar)
on Nov 13, 2003 at 20:04 UTC ( #306917=note: print w/ replies, xml ) Need Help??


in reply to Making a failed hash lookup return something other than undef

The code you posted has a problem when a hash entry exists but has a false value like 0 or undef. The straightforward way to test whether a hash entry exists is to use exists, e.g.:

my $var = exists $hash{'key'} ? $hash{'key'} : "unknown";

This is not particularly pretty, but works, and I don't know of a more succinct way to do the same thing.

-- Mike

--
XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
-- grantm, perldoc XML::Simpler


Comment on Re: Making a failed hash lookup return something other than undef
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-11-27 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (190 votes), past polls