Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: "print" of nonexistent element is actually altering a hash (updated)

by AnomalousMonk (Bishop)
on Feb 17, 2020 at 19:37 UTC ( #11113071=note: print w/replies, xml ) Need Help??


in reply to "print" of nonexistent element is actually altering a hash

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: "print" of nonexistent element is actually altering a hash
by haukex (Chancellor) on Feb 18, 2020 at 12:27 UTC

    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
Node Status?
node history
Node Type: note [id://11113071]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2020-05-30 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (173 votes). Check out past polls.

    Notices?