Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Uninitialized Value Warning

by Anonymous Monk
on Oct 22, 2003 at 21:23 UTC ( #301374=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

If I have code such as follows with use warnings on, what does it mean when I get "Use of uninitialized value in array element at line 6." and how do I get rid of it?
use strict; use warnings; my $count; my @array=(1,2,3); open(FILE,">/myfile.txt"); print FILE "$array[$count]\n"; close(FILE);

Replies are listed 'Best First'.
Re: Uninitialized Value Warning
by coreolyn (Parson) on Oct 22, 2003 at 21:29 UTC

    Hate to be so blunt but it means exactly what it says.

    You never gave $count a value. To get rid of it you could set my $count=0, but I'm not sure what that'll do for you. The way you have this code it looks like you're only trying to print "1" ( $array[0] ) to myfile.txt.

    What are you wanting this code to accomplish?

      Nothing, that was just an example of a problem I have seen in other scripts, however you provided the answer. Sorry to be stupid, but I didn't take the term "uninitialized" to mean null.

      Anyway, problem solved.

        It basically means you used a variable with the value undef in a string concatenation (which is what string interpolation does under the covers)... er, array index in this case, but the warning is emitted whenever an undefined value is used as if it were already defined. "Uninitialized" may be a little misleading because you can still get this warning even if the variable has had a value assigned to it before:

        use warnings; my $str = 'foo'; print "$str\n"; # ok $str = undef; print "$str\n"; # emits "uninitialized" warning

        By the way, you can see detailed explanations of pretty much all Perl warnings by checking out the perldiag documentation (run perldoc perldiag).

        -- Mike

        XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
        -- grantm, perldoc XML::Simpler

        It's not null. Null is a value (or at least the specific absence of one). It's uninitialized - there's never been a value assigned, and although Perl will try to do the right thing and assume 0 in numeric context or the empty string in string context, it really is just what it says: uninitialized.

        Update: Just remembered, some DBs use Null the way you did, I think.

        --Bob Niederman,

        All code given here is UNTESTED unless otherwise stated.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://301374]
Approved by davido
shmem missed his monkday yesterday :P - 12 years and way to go...
[1nickt]: Happy Monkday shmem. I was at my stepson's birthday party and thought of you.
[virtualsue]: holli about half
[choroba]: "Tatsächlich, das ist die Knochenstruktur eines Bettlers!" ??
shmem bows. Thank you
[marto]: slowclap.gif
[marto]: animated gif in question...
[1nickt]: Interesting article on the Coming Software Apocaplyse that I found while following one of eyepopslikeamosqui to's posts. Posits that no one can guarantee the quality of, or even understand, the 100 million lines of code in a mo
[holli]: you know, monk days are fun and all. but too many of them are bad for your health
[1nickt]: ...modern car. Suggests using TLA+ or other software modeling language to autogenerate code.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2017-11-21 12:56 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (301 votes). Check out past polls.