why perl vivify an anonymous array in @a?
Cos that's what you asked it to do :)
push @{$a[0]}, "foo"; say's treat the value in $a[0] as a reference to an array, and then push 'foo', onto the array located at that reference @{ <array_reference> }.
If the value of $a[ 0 ] is undefined, perl autovivifies an anonymous array, stores a reference to it in $a[ 0 ] and then pushes 'foo' into that.
Perl will only autovivify the anon. array if the value of the array element is undefined. if it has any value that would test as defined, including '' & 0, it will issue a warning.
W:\current>perl use strict; use Data::Dumper; my @a = ( 'fred' ); push @{ $a[ 0 ] }, 'foo'; print Dumper \@a; ^Z Can't use string ("fred") as an ARRAY ref while "strict refs" in use a +t - line 4. W:\current>perl use strict; use Data::Dumper; my @a = ( '' ); push @{ $a[ 0 ] }, 'foo'; print Dumper \@a; ^Z Can't use string ("") as an ARRAY ref while "strict refs" in use at - +line 4.
In reply to Re: push undefined and vivification
by BrowserUk
in thread push undefined and vivification
by oha
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |