simX
Unofficial Mac Genius
I'm going through the OReilly book called "Learning Perl", and they have an introduction section where they introduce some really basic stuff.
Here's the entire code for a short prog:
#!/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 "Randall") {
print "Hello, Randall! How good of you to be here!\n";
} else {
print "Hello, $name!\n"; # ordinary greeting
$secretword = $words{$name};
if ($secretword eq "") {
$secretword = "groucho";
}
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);
}
}
The problem is that it doesn't work, because it seems that if the name is not in the hash, then it doesn't initialize the string, instead of giving it a value of "" like the OReilly book claims. Is this a bug, is it something stupid on my part, or is this something deliberate that can be worked around? If it can be worked around.. how?
UPDATE: Neeeevermind.. apparently it does that when you give perl the "-w" parameter. When you don't, the program works. Whatever.
Here's the entire code for a short prog:
#!/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 "Randall") {
print "Hello, Randall! How good of you to be here!\n";
} else {
print "Hello, $name!\n"; # ordinary greeting
$secretword = $words{$name};
if ($secretword eq "") {
$secretword = "groucho";
}
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);
}
}
The problem is that it doesn't work, because it seems that if the name is not in the hash, then it doesn't initialize the string, instead of giving it a value of "" like the OReilly book claims. Is this a bug, is it something stupid on my part, or is this something deliberate that can be worked around? If it can be worked around.. how?
UPDATE: Neeeevermind.. apparently it does that when you give perl the "-w" parameter. When you don't, the program works. Whatever.
