Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Think about Loose Coupling
 
PerlMonks  

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 (Initiate)
on Feb 13, 2013 at 01:05 UTC ( #1018471=perlmeditation: print w/ replies, xml ) Need Help??

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.

Comment on A strange old perl bug: "Can't use an undefined value as an ARRAY reference" error given for a misleading/wrong line number
Select or Download Code
Re: A strange old perl bug: "Can't use an undefined value as an ARRAY reference" error given for a misleading/wrong line number
by BrowserUk (Pope) on Feb 13, 2013 at 01:42 UTC
    I think most would agree that it is easily identifyable as an issue in perl v5.8.8.

    This was a known limitation of older Perl's (pre-5.10 from memory).

    Basically, any warning or error resulting from an if or elsif condition within an if/elsif/else cascade was reported with the line number of the opening if.

    There are at least a couple of threads around here -- including one of my own -- that talk of this. If I succeed in digging one or more of them up, I'll update.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: A strange old perl bug: "Can't use an undefined value as an ARRAY reference" error given for a misleading/wrong line number
by Anonymous Monk on Feb 13, 2013 at 08:29 UTC
      Thanks to both of you for those references!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-04-17 01:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (437 votes), past polls