Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

undefined value as an ARRAY reference sometimes ok

by Anonymous Monk
on Dec 13, 2011 at 21:51 UTC ( #943427=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is this an inconsistancy (don't do it) or am I missing something?
#!/usr/bin/perl use strict; my $list = undef; my $item = $list->[0]; #ok my $list = get_no_list(); my $item = $list->[0]; #ok my $item = get_no_list()->[0]; #no way sub get_no_list { return; }

Comment on undefined value as an ARRAY reference sometimes ok
Download Code
Re: undefined value as an ARRAY reference sometimes ok
by chilledham (Pilgrim) on Dec 13, 2011 at 23:13 UTC

    See: What does Autovivify mean?

    Calling $list->[0] causes $list to autovivify. You can see this happening here:

    #!perl use strict; my $list = undef; print ref $list, "\n"; # should print only a blank line my $item = $list->[0]; print ref $list, "\n"; # should print ARRAY

      Maybe I missed it, but the refs you gave didn't explain why this didn't autovivify:
      my $item = get_no_list()->[0]; #no way

        Sorry for the lack of clarity in my first response. It's the attempt to dereference $list that causes the autovivification. In your last example, you are attempting to dereference your function call, not $list.

Re: undefined value as an ARRAY reference sometimes ok
by Anonymous Monk on Dec 14, 2011 at 00:31 UTC

    It is not an inconsistency

    only lvalues get autovivified, and undef is not an lvalue

    $ perl -le " sub dang : lvalue { undef } dang()=1; " Can't return undef from lvalue subroutine at -e line 1. $ perl -le " sub dang : lvalue { undef } dang()->[0] " Can't use an undefined value as an ARRAY reference at -e line 1. $ perl -le " undef->[0] " Can't use an undefined value as an ARRAY reference at -e line 1. $ perl -le " my $variable = undef; $variable->[0] "

    Even though undef can resemble an lvalue (hey, its on the left of an assignment),

    ( undef, undef, my $mode ) = stat 'filename';
    you can't assing a value to undef, so undef is not an lvalue
    $ perl -le " undef = 1; " Modification of a read-only value attempted at -e line 1.

    See References quick reference, autovivification, perlglossary#autovivification, autovivification, lvalue

Re: undefined value as an ARRAY reference sometimes ok
by ikegami (Pope) on Dec 14, 2011 at 06:47 UTC
    Autovifivication means that
    EXPR1->[ EXPR2 ]

    is short for

    ( EXPR1 //= [] )->[ EXPR2 ]

    This makes sense:

    $list //= []

    This doesn't make sense:

    get_no_list() //= []

    You can't assign to a non-lvalue function.

    If you want the first element of a list, you can use a list slice.

    ( some_list() )[0] # Returns the first element or an empty list.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://943427]
Approved by keszler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-12-21 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (105 votes), past polls