$hash{foo} ||= keys %hash;
At least, the results agree: the hash element exists (hence, it is counted in keys), before the assignment is called. My rule of thumb is: if you use a hash element as a R/W L-value, then it'll autovivify. It'll even autovivify in the for loop, if the loop body is empty:for($hash{foo}) { $_ ||= keys %hash; }
for($hash{foo}) { } # results in { 'foo' => undef }
I was surprised to see an exception when using a hash element as a sub parameter:
may or may not autovivify $hash{foo}, depending on what blackbox() does. For example:sub blackbox { ... } blackbox($hash{foo});
sub blackbox { } # no autovivify
sub blackbox { $_[0] = 123 } # results in { 'foo' => 123 }
Tested with perl 5.8.7
In reply to Re: Are "$hash{$_} ||= 1 + keys %hash" and variants well defined or not?
by bart
in thread Are "$hash{$_} ||= 1 + keys %hash" and variants well defined or not?
by demerphq
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |