Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Why doesn't this die with "Can't use an undefined value as an ARRAY reference"?"

by kikuchiyo (Hermit)
on Oct 18, 2017 at 20:56 UTC ( [id://1201619]=note: print w/replies, xml ) Need Help??


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:

#!/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;
...results in:
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' => [] };
  • Comment on Re^2: Why doesn't this die with "Can't use an undefined value as an ARRAY reference"?"
  • Select or Download Code

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

    Indeed, there does seem to be some apparent inconsistency there. The only thing I see about your examples that might be considered "consistent" is that those functions that take a scalar argument are the ones throwing an error - although I haven't yet expanded my list to more functions to see if this holds true elsewhere.

    $ perl -le 'printf "%10s %s\n",$_,prototype("CORE::$_")//"undef" for qw/pop shift map grep chomp lc localtime cos/' pop ;\@ shift ;\@ map undef grep undef chomp undef lc _ localtime ;$ cos _

    As for subroutine arguments, the elements of @_ being aliases to the actual parameters is documented and useful, but also one of those things that bites many people.

    In any case, this remains an interesting issue, thanks for continuing the investigation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1201619]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-25 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found