Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: "print" of nonexistent element is actually altering a hash

by haukex (Archbishop)
on Feb 18, 2020 at 12:27 UTC ( [id://11113095]=note: print w/replies, xml ) Need Help??


in reply to Re: "print" of nonexistent element is actually altering a hash (updated)
in thread "print" of nonexistent element is actually altering a hash

A very good point!

Note also that in deeply nested hashes, exists will autovivify intermediate levels of the hash in the process of testing the existence of a low level element.

To be nitpicky, it's not exists, but the hash accesses preceding the exists call.

Anyway, I just wanted to point out that the ugly-but-entirely-core way to avoid the autovivification in this example is:

print 'yes' if exists $hash{www} && exists $hash{www}{xxx} && exists $hash{www}{xxx}{yyy} && exists $hash{www}{xxx}{yyy}{zzz};

Although as I described in a recent thread, I try to keep my hash accesses fairly simple.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 23:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found