note
AnomalousMonk
<p>
Note also that in deeply nested hashes, [doc://exists] will autovivify <i>intermediate</I> levels of the hash in the process of testing the existence of a low level element <small>(<b>update:</B> but see [id://11113095|this] for further clarification)</SMALL>.
<c>
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 => {} } } }
</C>
This effect is also avoided with <c> no autovivification; </C> enabled.
<c>
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le
"no autovivification;
;;
my %hash;
print 'yes' if exists $hash{'www'}{'xxx'}{'yyy'}{'zzz'};
dd \%hash;
"
{}
</C>
</P>
<p>
<b>Update 1:</B> See also the recent discussion threads [id://11112778] and [id://11110437] - both by the same monk!
</P>
<p>
<b>Update 2:</B> To illustrate the behavior noted by [haukex] [id://11113095|here], consider:
<c>
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 } } } },
)
</C>
In every case except the <c>for</C>-loop (i.e., rvalue accesses), intermediate elements are created but not the lowest-level <c>'zzz'</C> element. In the <c>for</C>-loop case in which aliasing creates an lvalue access context, the <c>'zzz'</C> element <i>is</I> created.
</P>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-634253">
<hr><p>Give a man a fish<b>:</B> <c> <%-{-{-{-<</C></P>
</div></div>
11113064
11113064