|
Month Conversion Perl scriptDue: End of Friday LabSpecifications by Ian! Purpose:
Usage:$ ./monthconv [ month ] - Program reads the month from standard input if it is not supplied as an argument on the command line. - Program truncates input to three characters, so "june" and "Junk" would both convert to "6". - Program issues an error message if no conversion is possible. - Program issues an error message if no input is given. - If standard input is not a terminal device: - no prompt for input is issued - If standard output is not a terminal device: - output consists of *only* the numeric month (see examples below) Examples:$ ./monthconv December The month for 'December' is 12 $ ./monthconv jan The month for 'jan' is 1 $ ./monthconv Enter Month (Jan-Dec): NOVember The month for 'NOVember' is 11 $ ./monthconv ocTEMBER The month for 'ocTEMBER' is 10 $ ./monthconv garbage Cannot find month for input 'garbage' $ ./monthconv Enter Month (Jan-Dec): Cannot find month for input '' $ echo July | ./monthconv The month for 'July' is 7 $ ./monthconv june | cat 6 $ ./monthconv </dev/null No input; goodbye. $ echo `./monthconv septober` is the month 9 is the month Program Design:Use pseudocode and stepwise refinement to sketch how this program should operate before trying to write real Perl code for it. You must be clear on what the program does before you write it. get input process input print output Start from a small, working program and gradually add more features, one at a time, testing each one as you add it. DO NOT WRITE THE WHOLE THING AT ONCE. You will never get it working. #!/usr/local/bin/perl -w $input = <STDIN>; print "DEBUG input is '$input'\n"; Insert debugging print statements to verify that your input is what you think it is, and that your program is doing what you expect with the data. print "DEBUG input is '$input'\n"; $input =~ s/foo/bar/; print "DEBUG input is now '$input'\n"; New Perl Features:These statements illustrate the use of Perl features that you will require to make your program work. If you need more information on how a feature works, start with your course text and with "man perl". if ( -t STDIN ) { print "Prints only if standard input is a terminal"; } if ( -t STDOUT ) { print "Prints only if standard output is a terminal"; } $arg = shift; if ( defined($arg) ) { print "First argument on command line is '$arg'\n"; } $input = <STDIN>; if ( ! defined($input) ) { die "No input found; bye-bye.\n"; } Hand In:
|