shemp has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I was playing with some code that uses an arrayref in various places, and started getting errors when i added some code and the arrayref was undef. Originally, there was something like: (all code has strict & warnings on, these are just snippets)
my $arrayref = getArrayref(); # this sometimes returned undef foreach my $value (@$arrayref) { ... }
This worked ok, even when $arrayref == undef. So then i added an if conditional to the method:
if ( @$arrayref ) { ... }
This resulted in a fatal error:

Can't use an undefined value as an ARRAY reference at ...

My co-worker and I discussed this and decided that it was an rvalue versus lvalue issue. But our resolution still doesn't sit very well in my mind.


Update:This is incorrect, it creates a fatal error.
If i put this line before the if, then things are ok:
@$arrayref
end update
But thats not cool, and results in a warning too.

Anyway, in the end i did the if statement as:

if ( defined($arrayref) && @$arrayref ) { ... }
(perhaps i should be doing a ref test also, but the value of $arrayref is always set by the same method.)

So im looking for more insight into exactly why this is all behaves like it does. I sort of understand, but not completely.


I use the most powerful debugger available: print!

Replies are listed 'Best First'.
Re: Using undef scalar as arrayref
by ikegami (Patriarch) on Aug 12, 2005 at 20:38 UTC

    You've been the victim of auto-vivification:

    use strict; use warnings; my $arrayref; foreach (@$arrayref) { } print($arrayref\n"); # ARRAY(0x1abefc0)

    scalar @$arrayref doesn't autovivify, therefore if (@$arrayref) { ... } gives an error.


    If you don't distiguish between undefined and 0 elements, you could create a 0-element array when the function returns undef:

    use strict; use warnings; my $arrayref = getArrayref(); # this sometimes returned undef # Manually vivify it. $arrayref ||= []; if (@$arrayref) { ... }

    Otherwise, yes, you have to check if it's defined (or simply true) first:

    use strict; use warnings; my $arrayref = getArrayref(); # this sometimes returned undef if ($arrayref && @$arrayref) { ... }
Re: Using undef scalar as arrayref
by Joost (Canon) on Aug 12, 2005 at 20:15 UTC
    Weird. I've never noticed this before. There is one problem with your post (unrelated to the for (@$arrayref) working with $arraryref being undef):
    If i put this line before the if, then things are ok:
    @$arrayref;
    But thats not cool, and results in a warning too.
    That can't work at all:
    #!/usr/bin/perl -w use strict; my $arrayref; @$arrayref; # line 5 print "ok";
    Useless use of a variable in void context at test.pl line 5. Can't use an undefined value as an ARRAY reference at test.pl line 5.

    Anyway, I suggest using

    if ($arrayref) { ... }
    if $arrayref can only be false (undef) or a real ref.

    update: about the if ($arrayref) test: if, as you say, arrayref should be false or an arrayref, there's no reason to do an defined() and ref() test, because if the $arrayref is true it should be an arrayref, and if it's not, there's nothing you can usefully do, except throw an exception, which is what will happen by default:

    #!/usr/bin/perl -w use strict; my $arrayref = 1; for (@$arrayref) { print "ok\n"; }
    Can't use string ("1") as an ARRAY ref while "strict refs" in use at t +est.pl line 5.
      Ack, you're right about what you noticed about line 5. I goofed. You're also right about the defined test.

      The only caveat i can think of is that if the $arrayref got set to an empty arrayref, i'd still not want to do the if conditional (in this situation). So i think the final will be:

      if ( $arrayref && @$arrayref ) {
      which will throw an exception if its not an arrayref.

      I use the most powerful debugger available: print!