No such thing as a small change | |
PerlMonks |
Re: Explaining Autovivicationby ikegami (Patriarch) |
on Jun 12, 2008 at 08:41 UTC ( [id://691609]=note: print w/replies, xml ) | Need Help?? |
When a scalar ($foo, $foo[$i], $foo{$k}) is undefined and it is used as a reference (@$foo, $foo->[$i], ${$foo}[$i], %$foo, $foo->{$k}, ${$foo}{$k}, $$foo), two things can happen.
One possibility is that you get a warning. This occurs when you are trying to read from the dereferenced structure. (Technically, when the reference is used as an rvalue).
The other possibility is that the reference is autovivified. That means the appropriate structure (array, hash or scalar) is constructed and a reference to it is stored in the variable. This occurs when you are trying to write to the dereferenced structure. (Technically, when the reference is used as an lvalue).
Note then creating an alias to the dereferenced structure is an lvalue, and results in autovivification.
In Section
Seekers of Perl Wisdom
|
|