When do the dereferencing and autovivication occur?
Autvivification occurs any time you use a variable whose value is undef as a hash or array reference in a lookup or assignment statement. Simple example:
>perl -e"my $x=undef; $y++ if $x->{foo}; print $x" HASH(0x15d56e0)
Notice how the if $x->{foo} autovivified $x into a hash reference. When you have a multiple key lookup this rule applies to each value in turn until you get to the last lookup.
This behaviour is very powerful and is used all the time in perl. About the only thing you need to keep in mind, is when doing a lookup where you care about autoviv you have to check each value for undef first. Iow:
if ($x and $x->{foo} and $x->{foo}{bar} and exists $x->{foo}{bar}{baz} +) { }
It doesn't take long to get used to working with complex perl variables and to learn strategies that minimize how often you need to do stuff like this, with experienced perl programmers only rarely having to do so.
In reply to Re^5: Why does exists cause autovivication?
by demerphq
in thread Why does exists cause autovivication?
by Argel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |