Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl newbie: "Use of uninitialized value in string ne at"

Status
Not open for further replies.

ponetguy2

MIS
Aug 28, 2002
442
US
Hello All,

Please forgive me if this is a stupid question. I'm new to Perl and I'm not familiar w/ the err messages. I'm trying to follow O'Reilly's Learning Perl. I pretty much copied the sample script, by I'm getting an error message. Can someone please explain?

Here is the script:


#!/usr/bin/perl -w
%words = qw (
fred camel
barney llama
betty alpaca
wilma alpaca
);
print "What is your name? ";
$name = <STDIN>;
chomp ($name);
if ($name eq "Randal") {
print "Hello, Randal! How good of you to be here!\n";
} else {
print "Hello, $name!\n"; # ordinary greeting
$secretword = $words {$name}; # get the secret word
print "What is the secret word? ";
$guess = <STDIN>;
chomp ($guess);
while ($guess ne $secretword) {
print "Wrong, try again. What is the secret word? ";
$guess = <STDIN>;
chomp ($guess);
}
}

Here is the message :

sun3# ./hello_world2.pl
What is your name? eric
Hello, eric!
What is the secret word? camel
Use of uninitialized value in string ne at ./hello_world2.pl line 19, <STDIN> line 2.
Wrong, try again. What is the secret word?

Please explain.


"Not all OSs suck, it's just that some OSs suck worse than others"


 
Following just the code flow, here is what's going on:

Code:
$name = <STDIN>;
chomp ($name);               # You entered 'eric'
print "Hello, $name!\n";     # ordinary greeting
$secretword = $words{$name}; # undef - $word{'eric'} is not defined
print "What is the secret word? ";
$guess = <STDIN>;
chomp ($guess);
while ($guess ne $secretword) {  # <-- error here $secretword is not defined.
}

Lots of options on getting around it. One possibility
Code:
%words = qw (
   fred    camel
   barney  llama
   betty   alpaca
   wilma   alpaca
   default snorkasaurus
);
### then below

$secretword = (defined $words{$name}) ? $words{$name} : $words{'default'}
This obviously isn't a great idea as default secretwords (eg passwords) never really are. But for the sake of an example.
 
This will avoid the warning:

Code:
$secretword = $words {$name} || '';





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Take care with the above method. Here is an example of where I've run into downstream problems
Code:
$hsh1{'fred'}   = 'camel' ;
$hsh1{'barney'} = 'llama' ;

$hsh2{'fred'}{'icon'}   = 'camel' ;
$hsh2{'barney'}{'icon'} = 'llama' ;
$name = 'eric' ;

if (exists $hsh1{$name}) { print "test 1: $name exists\n";};
$secretword = $hsh1{$name} || "" ;
if (exists $hsh1{$name}) { print "test 2: $name exists\n";};

if (exists $hsh2{$name}) { print "test 3: $name exists\n";};
$secretword = $hsh2{$name}{'icon'} || "" ;
if (exists $hsh2{$name}) { print "test 4: $name exists\n";};

From the above, "test 4" will print out. (i.e. exists $hsh2{$name} will evaluate to true) -- This has caused me downstream logic errors when it was assumed $hsh2{$name} exists and was properly initialized.
 
Good point. But the variable is properly initialized, it just has an empty/false value although it is defined.

Rarely, but sometimes, there is no difference between:

if ($hash{'key'}) {....}

and:

if (exists $hash{'key'}) {....}

You should not rely on "exists" to assume the value is true, only that the hash key exists in the hash table. The time to use "exists" is to make sure a mutli-level hash key does not autovivify if you don't want it to if you do something like:

if ($hash{'foo'}{'bar'}) {....}

in which case $hash{'foo'}{'bar'} will spring into existence if it was not already in the hash table. But the same is not true of a single-level hash.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
thank you everyone for the help. i'm a newbie to perl. i'll try everyone's suggestion. :)

"Not all OSs suck, it's just that some OSs suck worse than others"


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top