I’m having tough time in understanding why the following works:
my $array_reference;
foreach $element (@{$array_reference}) {
# some code
}
while the following does not work
my $array_reference;
if (scalar (@{$array_reference}) {
# some code here
}
I understand that perl brings to life (auto-vivifies) undefined reference. But I am still confused as in why the latter code segment throws FATAL.
Dereferences autovivify in lvalue context (meaning when a modifiable value is expected), and foreach create an lvalue context.
The last two create an lvalue context because they need a value to which to alias
$_[0]and$_(respectively).