Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

what does "use of uninitialized value" really mean?

by rocketperl (Sexton)
on Jul 13, 2013 at 12:29 UTC ( #1044145=perlquestion: print w/ replies, xml ) Need Help??
rocketperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi monkers, I am still wondering and cant really figure out why this warning "use of uninitialized value in array" occurs. Inspite of this i do get my expected output correctly. I have initialized all my variables globally and use them in subs. How do i rectify this warning expect for "no warnings 'uninitialized'"? and what is this warning trying to tell? thanks a lot

Comment on what does "use of uninitialized value" really mean?
Re: what does "use of uninitialized value" really mean?
by moritz (Cardinal) on Jul 13, 2013 at 12:50 UTC

    It means that a an array item you access has the value undef. This can occur for example if you index below the end of the array, or access an element that you never assigned to, or if the value that you assigned to it was simply undef.

    Inspite of this i do get my expected output correctly.

    If you use undef as a string, it acts like the empty string. If you use it as a number, it acts like 0 (except that it warns in both cases). So there are quite a few cases where the program works as it should, even though it emits those warnings.

    But in the general case, the warning might be a hint towards a programming error.

Re: what does "use of uninitialized value" really mean?
by AnomalousMonk (Abbot) on Jul 13, 2013 at 12:50 UTC

    Of course, you don't give us the exact error message or short, self-contained code that will produce it. Is it something like this?

    >perl -wMstrict -le "my @ra = qw(a b c d); my $uninitialized; print $ra[3]; print '----------'; print $ra[$uninitialized]; print $ra[undef]; " d ---------- Use of uninitialized value $uninitialized in array element at -e line +1. a Use of uninitialized value in array element at -e line 1. a
Re: what does "use of uninitialized value" really mean?
by toolic (Chancellor) on Jul 13, 2013 at 12:53 UTC

    Tip #1 from the Basic debugging checklist: use diagnostics will give a more verbose explanation of the warning. See also perdiag. You eliminate the warning by assigning a value to a variable. Perhaps you are confusing variable declaration with initialization. You should show a code example of where you get the warning.

Re: what does "use of uninitialized value" really mean?
by smls (Friar) on Jul 13, 2013 at 12:54 UTC

    "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]
Re: what does "use of uninitialized value" really mean?
by Laurent_R (Parson) on Jul 13, 2013 at 14:43 UTC

    You've received several good answers. If you are still puzzled with the reason and need more help, you should post your code (or, if the code is large, rather the smallest subset of your code producing the warning) as well as the complete warning message.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-11-27 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (187 votes), past polls