in reply to Re: Why doesn't this die with "Can't use an undefined value as an ARRAY reference"?"
in thread Why doesn't this die with "Can't use an undefined value as an ARRAY reference"?"
Thanks for the investigation!
Some additional observations:
no autovivification qw/store/; causes the offending part (of your minimal example) to die with "Can't vivify reference at -e line 1.".Reading the autovivification module's documentation gave me a hint: lvalue context. Searching that led me to a Perlmonks article from 12 years ago: Autovivification of scalars in sub calls Those who don't know history are doomed to repeat it, apparently.
The key phrase seems to be "arguments to subs are lvalues" and apparently this is a feature. Still, I find it surprising and confusing.
The linked thread mentions that "incidentally, builtin functions do not provide a similar service" (i.e. autovivification for their arguments) - this is not unconditionally true:
...results in:#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $h={}; for my $x ("pop", "shift", "map {1}", "grep {1}", "chomp", "lc", "loca +ltime", "cos") { my $str = $x.q/ @{$h->{'/ . $x . qq/'}}\n/; print $str; eval $str; print "\t".$@ if $@; } print Dumper $h;
pop @{$h->{'pop'}} shift @{$h->{'shift'}} map {1} @{$h->{'map {1}'}} grep {1} @{$h->{'grep {1}'}} chomp @{$h->{'chomp'}} lc @{$h->{'lc'}} Can't use an undefined value as an ARRAY reference at (eval 7) lin +e 1. localtime @{$h->{'localtime'}} Can't use an undefined value as an ARRAY reference at (eval 8) lin +e 1. cos @{$h->{'cos'}} Can't use an undefined value as an ARRAY reference at (eval 9) lin +e 1. $VAR1 = { 'chomp' => [], 'pop' => [], 'map {1}' => [], 'grep {1}' => [], 'shift' => [] };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Why doesn't this die with "Can't use an undefined value as an ARRAY reference"?"
by haukex (Archbishop) on Oct 18, 2017 at 22:19 UTC |