Problems 11
Home Up Review Problems 1 Problems 2 Problems 3 Problems 4 Problems 5 Problems 6 Problems 7 Problems 8 Problems 9 Problems 10 Problems 11 Problems 12 Problems 13 Problems 14 Problems 15 Problems 16 Subnets

 

Month Conversion Perl script

Due: End of Friday Lab

Specifications by Ian!    

Purpose:

Write a Perl script to convert a character month into a numeric month.  January is 1; December is 12.  See the Examples for the exact output format.

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:

Hand in your Perl Script along with samples, similar to the Examples, above, that show that your script can handle any input in a reasonable manner.