Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
An alternate way to test is by using the ref function, this would avoid autovivification in a way similar to the on the first key then the existence of key beneath it option suggested. That other method might not work correctly because 'bar' may be a real key, but its content might not be an anonymous hash. In other words the key exists so the first test would pass and then cause an error similar to:
Can't use string ("key bar's value") as a HASH ref while "strict refs"
The sub routine suggested by Aristotle suffers from the same problem. However there is no mention in the orignal post as to how complex the data structure might be, but I in my own experience I have been biten by similar problems.

Perl fails as early as possible in the truth process, that is the ref fails so it doesn't bother attempting to try the second operation, thereby avoiding the problem.
Here it is with the ref and no value for 'bar'
#!/usr/bin/perl use strict; use warnings; my %foo; if(exists($foo{qux})) { print "\$foo{qux} exists"; } if(ref $foo{bar} eq 'HASH' && exists($foo{bar}{baz})) { print "\$foo{bar}{baz} exists\n"; } if(exists($foo{bar})) { print "\$foo{bar} popped into existence\n"; }
Now if we assign a scalar value to bar and try again it still works.
#!/usr/bin/perl use strict; use warnings; my %foo; $foo{bar} = "key bar's value"; if(exists($foo{qux})) { print "\$foo{qux} exists"; } if(ref $foo{bar} eq 'HASH' && exists($foo{bar}{baz})) { print "\$foo{bar}{baz} exists\n"; } if(exists($foo{bar})) { print "\$foo{bar} popped into existence\n"; }
And finally with exist && exist to produce our error.
#!/usr/bin/perl use strict; use warnings; my %foo; $foo{bar} = "key bar's value"; if(exists($foo{qux})) { print "\$foo{qux} exists"; } if(exists($foo{bar}) && exists($foo{bar}{baz})) { print "\$foo{bar}{baz} exists\n"; } if(exists($foo{bar})) { print "\$foo{bar} popped into existence\n"; }

In reply to Re: Autovivification with hash of hashes by trs80
in thread Autovivification with hash of hashes by Mr_Person

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found