st_possenti has asked for the wisdom of the Perl Monks concerning the following question:

I keep getting a warning about line 15 being uninitialized. I know I am missing something. I have been teaching myself perl with the O'rielly books. Any help would be great.
#!/usr/bin/perl -w use strict; use warnings; use diagnostics; my %books; my $someone; $books{"fred"} = 3; $books{"wilma"} = 1; $books{"barney"} = 0; $books{"pebbles"} = undef; if ($books{$someone}) { print "$someone has at least one book checked out.\n"; }

Replies are listed 'Best First'.
Re: Uninitialized value!
by Coruscate (Sexton) on Feb 17, 2003 at 07:57 UTC

    my $someone; will create the variable so that you can put a string, number, reference, or some other perlish piece of data into $someone. In your snippet of code, however, you do not place a value into that scalar variable. As well, the '-w' on the first line of the script and the 'use warnings' line do more or less the same thing, so you can always opt to keep the '-w' and remove the 'use warnings' line. There are slight differences between the two methods of enabling warnings, but I won't get into the gory details. In most cases, either one will do just fine. And to make your life even easier, you can place your $books{'fred'} = 3; $books{'wilma'} = 1; declarations within the hash when you create it. A modified version (with an initialized $someone variable might look something like this:

    #!/usr/bin/perl -w use strict; my %books = ( fred => 3, wilma => 1, barney => 0, pebbles => undef ); my $someone = 'wilma'; if ($books{$someone}) { # Let's fancy it up: show how many books they have! print "$someone has $books{$someone} books checked out\n"; }


    Update: P.S: Welcome to the Monastery! ++ to you on your first post here, hopefully it will be the first of many. I'd like to congratulate you on that script. You used warnings, strict, and somehow managed to figure out that we like to use <code> tags around here. Hope to see you around some more :)


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, reply to this node or /msg me to tell me what is wrong with the post, so that I may update the node to the best of my ability. If you do not inform me as to why the post deserved a downvote, your vote does not have any significance and will be disregarded.

Re: Uninitialized value!
by hotshot (Prior) on Feb 17, 2003 at 07:42 UTC
    you first need to initialize $someone before you can use it in this context (in the 'if' statement), I believe you're getting the name from stdin, file or something, just assign it to some one and you get your thing

    Hotshot
Re: Uninitialized value!
by parv (Parson) on Feb 17, 2003 at 08:07 UTC

    What the hotshot said ... plus you could add a test for a thingy being defined/existed in place of or in addition to the test for a "bare" truth value. (Mind you existence test will work iff $someone is defined.) Below are some, if not all, of the ways to test various types of truth...

    if ( $someone && $books{$someone} ) {...} if ( defined $someone && $books{$someone} ) {...} if ( defined $someone && defined $books{$someone} ) {...} if ( defined $someone && exists $books{$someone} ) {...} if ( exists $books{$someone} ) {...}