Here's how I would have written it. (This code has been lightly tested.)
use strict;
use warnings;
$|++;
my %acro = (
HTML => "Hypertext Markup Language",
ICBM => "Intercontinental Ballistic Missile",
EEPROM => "Electronically-erasable programmable read only memory",
SCUBA => "Self Contained Underwater Breathing Aparatus",
FAQ => "Frequently Asked Questions",
LCARS => "Library Computer And Retrieval System",
NASA => "National Aeronautical and Space Administration",
);
INPUT: {
print "Please enter an acronym: ";
chomp( $_ = <STDIN> );
$_ = uc or redo INPUT;
last INPUT if /^Q/;
unless (exists $acro{$_})
{
print "I don't know what '$_' means.\n";
redo INPUT;
}
print "$_ ($acro{$_})\n";
redo INPUT;
}
Some explanations:
- Always begin programs with the top two lines. It's easier to have the compiler do the bookkeeping for you.
- The $|++ is to disable output buffering. (q.v. Suffering from Buffering for more info.)
- Then, I set up the lookup table for the acronyms.
- Now, here's the goofy thing. You'll notice I'm using a named block and using last and redo. (I can't use next cause I'm not in a loop, but redo does something similar.) Sometimes, a certain construct makes a given algorithm easier to express for a given person. I like using named blocks and redo to handle keyboard input. Others like while-loops, goto, and all sorts of other ideas. I like named blocks.
- Now, I assign to $_, which has the fancy name of "local topic". This is the default variable for many functions, including regexes.
- I want to compare case-insensitively, so I uc the string. (uc defaults to using $_.) Now, if there is nothing, uc will return the empty string. This allows me to use logic short-circuits to redo the loop if nothing was entered. (Try it!)
- I allow for a quit scenario by stopping if anything that begins with 'Q' or 'q' is entered. (You might want to change this to allow acronyms that begin with 'Q'.)
- I use the exists function to have the hash do the lookup for me (instead of me coding a for loop). This also has the benefit of keeping my hash clean. If it doesn't exist, print a message and redo the block.
- If it does exist, print the acronym and definition, then redo the block.
If you have any questions, please don't hesitate to ask!
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested