Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1044151]
help
Chatterbox?
[GotToBTru]: yes, it turns out I am
[LanX]: http://perldoc. perl.org/perlsub. html#Prototypes
[GotToBTru]: that page, strangely enough, never mentions that construct
[LanX]: ; is for optional prototypes
[LanX]: & is codeblock @ array
[GotToBTru]: okay, that is there in the text, not in literal use in any of the examples
[GotToBTru]: the text suggest it is redundant before @
[LanX]: propably ... I also notice new stuff

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2017-01-18 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (164 votes). Check out past polls.