MC logo

CSc 231 Assignment 2

^  CSc 231 Perl Assignments

70 pts

Well, Hello There

Due: Feb 15

Modify the advanced technology hello world program to obey the following additional options:
-M filename
  Use contents of the file filename as the message. If the file contains multiple lines, append those lines together separated by a single space. If the file cannot be opened, die with an appropriate message. If both the -M and -m options are used together, die with a (different) appropriate message.
-o filename
  Write the message output to the file given by filename instead of standard output. If the file cannot be opened for writing, die with an appropriate error message. If this option is present, all program output should go to the indicated file, except output generated by the -h option, which always goes to standard output, and error messages, which always go to standard error. Note that die writes to standard error.
-hr
  Print lines of dashes before and after the message. They should be the same length as the message, and indented the same amount. For example, perl hellop.pl -n 2 -m "Everybody wants to be a cat." -hr -i 3 should produce
   ----------------------------
   Everybody wants to be a cat.
   Everybody wants to be a cat.
   ----------------------------
In addition to the option processing, when neither the -m or -M flags are given, check to see if the environment variable DEFAULT_HELLO_MSG is set. If so, use it as the message instead of Hello, World!.

Hints

For the environment part, check for the name in %ENV. Say exists $ENV{"DEFAULT_HELLO_MSG"} to see if the variable has been set. In order to test your program, you must set the variable. On sandbox, you can use a command like:

export DEFAULT_HELLO_MSG="Watch for trolls."

From a Windows or DOS shell, the command set (with the same syntax otherwise) should work.

Submission

When your program is properly indented, commented, and works, submit over the web here.