Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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 (update: but see this for further clarification).

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my %hash; print 'yes' if exists $hash{'www'}{'xxx'}{'yyy'}{'zzz'}; dd \%hash; " { www => { xxx => { yyy => {} } } }
This effect is also avoided with  no autovivification; enabled.
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "no autovivification; ;; my %hash; print 'yes' if exists $hash{'www'}{'xxx'}{'yyy'}{'zzz'}; dd \%hash; " {}

Update 1: See also the recent discussion threads Sometimes undef is initialized and sometimes not when hash values are fed to grep and unexpected modify hash in a distance with grep { $_ } - both by the same monk!

Update 2: To illustrate the behavior noted by haukex here, consider:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my %h; ;; $h{'www'}{'xxx'}{'yyy'}{'zzz'}; dd 'access in void context', \%h; %h = (); dd 'assigning empty list really does clear hash', \%h; ;; my $x = $h{'www'}{'xxx'}{'yyy'}{'zzz'}; dd 'access in assignment (rvalue) context', \%h; %h = (); ;; 1 if $h{'www'}{'xxx'}{'yyy'}{'zzz'}; dd 'access in boolean context', \%h; %h = (); ;; 1 for $h{'www'}{'xxx'}{'yyy'}{'zzz'}; dd 'access in aliased (lvalue) context', \%h; " Useless use of hash element in void context at -e line 1. ("access in void context", { www => { xxx => { yyy => {} } } }) ("assigning empty list really does clear hash", {}) ( "access in assignment (rvalue) context", { www => { xxx => { yyy => {} } } }, ) ( "access in boolean context", { www => { xxx => { yyy => {} } } }, ) ( "access in aliased (lvalue) context", { www => { xxx => { yyy => { zzz => undef } } } }, )
In every case except the for-loop (i.e., rvalue accesses), intermediate elements are created but not the lowest-level 'zzz' element. In the for-loop case in which aliasing creates an lvalue access context, the 'zzz' element is created.


Give a man a fish:  <%-{-{-{-<


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

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 studying the Monastery: (2)
As of 2024-04-19 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found