From perlref (emphasis mine): "... references could spring into existence when in an lvalue context. ... This process is called autovivification." foreach provides lvalue context to what it is looping over: "If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop. ... the foreach loop index variable is an implicit alias for each item in the list that you're looping over."
$ perl -wMstrict -MData::Dump -e ' @{$a->{list}} = (); dd $a' { list => [] } $ perl -wMstrict -MData::Dump -e ' for (@{$a->{list}}) {}; dd $a' { list => [] } $ perl -wMstrict -MData::Dump -e ' my @x = @{$a->{list}}; dd $a' Can't use an undefined value as an ARRAY reference at -e line 1.
In reply to Re: Array dereference in foreach()
by haukex
in thread Array dereference in foreach()
by pme
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |