in reply to Re^4: Why does exists cause autovivication?
in thread Why does exists cause autovivication?
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: Why does exists cause autovivication?
by Argel (Prior) on Dec 31, 2007 at 19:41 UTC | |
by demerphq (Chancellor) on Jan 02, 2008 at 11:01 UTC | |
by Argel (Prior) on Jan 02, 2008 at 20:45 UTC | |
by shmem (Chancellor) on Jan 02, 2008 at 21:31 UTC |