http://www.perlmonks.org?node_id=1018471

Here's a little program:
#!/usr/bin/perl use strict; my $undef_ref = undef; my $array_ref = [1,2,3]; if (!scalar @{$array_ref}) { print "foo\n"; } elsif (!scalar @{$undef_ref}) { print "bar\n"; }

Running this code under perl v5.8.8 gives me:

Can't use an undefined value as an ARRAY reference at arrayref2.pl line 8.

...which is the check on the defined array reference.

Running this code under perl v5.10.1 (*) (with 59 registered patches), the next newest perl I had available, produces this output:

Can't use an undefined value as an ARRAY reference at arrayref2.pl line 11.

...which is the check on the undefined array reference.

When laid out this way, I think most would agree that it is easily identifyable as an issue in perl v5.8.8.

However, when I ran across this while working on some legacy perl code at my job (hence the old version(s)) and this situation was masked by much more complex conditionals that were much more deeply nested, this was a real pain to figure out! I was especially thrown off at first when I changed the equivalent of the first if to:

if (0) {}

...and it still gave the same undefined array reference error for that line! Though that is also part of what led me to discovering the issue ultimately.

I was unable to turn up anything quite like it searching the web, as for obvious reasons, most of the results were dealing with situations in which the line number reported actually contained an undefined variable being used as an array reference.

I thought I would write this up here on the off-chance that it might turn up amongst search results for someone else if they run into this same issue.