Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

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


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

Actually, auto-vivification of hash entries doesn't happen unless you do something like take a reference to the hash entry... for example by referring to something more deeply nested in the non-existent hash entry:

#!/usr/bin/perl -l my %hash; my $foo = $hash{'foo'}; print exists $hash{'foo'} ? "yes" : "no"; # prints "no" my $bar = $hash{'bar'}->{'xxx'}; print exists $hash{'bar'} ? "yes" : "no"; # prints "yes" my $baz = \$hash{'baz'}; print exists $hash{'baz'} ? "yes" : "no"; # prints "yes"

-- Mike

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


Comment on Re: Re: Making a failed hash lookup return something other than undef
Download Code
Re: Re: Re: Making a failed hash lookup return something other than undef
by shenme (Priest) on Nov 13, 2003 at 20:33 UTC
    However, the hash itself may be auto-vivified in spite of using exists().   In fact, the hash will be auto-vivified by the use in exists()!   This may be what davido was thinking about.   Check perlref for examples of how the end-point (the hash element) is protected from auto-vivification by the above constructs, by the leading components of the hash expression are not.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2015-07-01 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (24 votes), past polls