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.
|