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.


In reply to A strange old perl bug: "Can't use an undefined value as an ARRAY reference" error given for a misleading/wrong line number by elumelum

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.