Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: what does "use of uninitialized value" really mean?

by smls (Friar)
on Jul 13, 2013 at 12:54 UTC ( #1044151=note: print w/ replies, xml ) Need Help??


in reply to what does "use of uninitialized value" really mean?

"Use of uninitialized value in array element" occurs when you use undef as an array index:

use strict; use warnings; my @array = (5, 10); my $index = undef; print $array[$index];

It would still output "5" (in addition to the warning), by interpreting undef as 0 - which may or may not be what you intended (hence the warning).

To get rid of the warning, make sure that any variable or expression that is used as an array index will never be undef. If the undef values occured by accident, fix the bug. If you're sure that undef values are no accident and that they should indeed be interpreted as 0, you can make that explicit for example using the // operator:

print $array[$index//0]


Comment on Re: what does "use of uninitialized value" really mean?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-12-25 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (160 votes), past polls