Place the answer to problem 1 in a file named answers. The answers to the other problems should be put in the files specified by the problem.

  1. (5 points) Explain what is accomplished by the following line of Perl code:
    	$line =~ s/<[^>]*>//g;
    Show a simpler way to write the search pattern.

  2. (15 points) Write a Perl program named center.perl that searches for all header tags of the form <h1>, <h2>, or <h3> that start at the beginning of a line in an html file. Place the tags <center> and </center> around all such headers. The headers may span more than one line in the file. For example, your program should replace:
    	<h1> An Introduction to Perl
    		and Python 101 </h1>.
    	<center><h1> An Introduction to Perl
    		and Python 101 </h1></center>.
    You do not have to worry about nested header tags. You should read the file name from the command line and write to stdout.

    hint: You can use the following combination of modifier flags at the end of your search pattern to write a single pattern for performing the substitutions:

    1. s: wrap the search past newline characters
    2. m: let the ^ match expressions starting after a /n character in a string
    3. g: perform multiple substitutions

  3. (20 points) In this problem you are going to create a web page that takes a person's address and takeout order and communicates it to the server. You are also going to write a Perl program that creates a dynamic html page which echos the address and the calculated cost of the order.

    Here is some useful information for both this problem and the following problem:

    1. Here are some snapshots of what your pages might look like.
    2. Here are some frequent errors that will cause the brower to tell you that you can't run your cgi script.
    3. Here are some detailed directions to use for storing and testing your Perl .cgi scripts:

      1. Create a cgi-bin subdirectory in your www-home directory
      2. Put your perl .cgi scripts in the cgi-bin directory. Make sure they end with the extension .cgi rather than .pl.
      3. Make the scripts world readable and executable. The easiest way to do that is to use "chmod 0755 filename.cgi" where filename is the filename of your .cgi script.
      4. The URL for reaching your scripts should be "". You will put this URL in the action attribute of your form element.
      5. You may also need to access your initial web page (i.e., icecream.html), by typing in and the path to your icecream.html file.

  4. (20 points) You are going to modify the above problem by saving the order information, rather than immediately displaying a confirmation page. When the user hits the submit button for the order page, you will generate a dynamic web page that asks the user for credit card information. When the user provides the credit card information, you will generate a second dynamic web page that echos the order information, the credit card information, and thanks the user for the order. Here are some screen snapshots that illustrate the order sequence. The credit card information should show the last four digits of the credit card with *'s for the first 12 digits. The credit card number should be formatted as '****-****-****-3685'. Do not worry about type-checking the data.

    Here are specific instructions for this problem:

    1. Write a cgi script, named icecream_h.cgi that uses hidden fields to 1) save the information entered on the first web form (the order form), and 2) dynamically generates a credit card form that contains the following widgets:

      1. select between MasterCard, Visa, and American Express. Make MasterCard be the default selection.
      2. enter a credit card number using a protected password type field. Tell the user to enter the credit card number as consecutive digits without spaces or dashes.
      3. select an expiration date from a month menu with the months January through December and a year menu with the years 2008 through 2012. Make January and 2008 be the default selections for these menus.
      4. submit the form.

    2. Copy your icecream.html file from problem 3, rename it icecream_h.html, and make its action attribute call icecream_h.cgi.

    3. Finish up by writing a Perl cgi script named icecream-hidden.cgi that generates an order confirmation page that repeats the order information, the credit card information, and thanks the customer for the order. icecream-hidden.cgi should use the hidden fields stored in the credit card form to retrieve the order information.

    4. Now repeat the above steps, except use cookies instead of hidden fields. Name your files icecream_c.html, icecream_c.cgi, and icecream-cookie.cgi. icecream_c.cgi will be the action for icecream_c.html and it will use cookies to store the order information. icecream-cookie.cgi will be the action for the credit card page and it will generate the confirmation page. It should use the cookies to retrieve the order information.

  5. (20 points) Write a perl script named backup.perl that takes the name of a directory as a command line argument and moves all files that end with a tilde (~) to a file in that directory named backup. If the backup directory does not already exist then create the backup directory. Your script should check to make sure that the directory for which backups are being requested is a valid directory and print an appropriate error message if it is not. As an example, suppose I have a directory named /home/bvz/cs594/notes that produces the following ls listing:
    #perl2.html#            perl23.html~            perlsystem.html~
    perl1.html              perlfile.html           perlweb.html
    perl1.html~             perlfile.html~          perlweb.html~
    perl2.html              perlsys.html            references.html
    perl23.html             perlsys.html~           references.html~
    After running backup.perl on this directory, it should produce the following ls listing:
    backup           perl1.html      perl23.html     perlsys.html    references.html
    #perl2.html#     perl2.html      perlfile.html   perlweb.html
    and backup should produce the listing:
    perl1.html~             perlsys.html~           references.html~
    perl23.html~            perlsystem.html~
    perlfile.html~          perlweb.html~
  6. (20 points) Write a perl script named count_bytes that takes a directory name as a command-line argument and prints the size of each .perl file in both that directory and recursively in any sub-directories. For example: UNIX> perl count_bytes /Users/bvz /Users/bvz/rewrite.perl: 704 /Users/bvz/animation/lighten.perl: 5888 /Users/bvz/animation/darken.perl: 1192 /Users/bvz/cs594/notes/example.perl: 1802 Although you can keep track of the directory path yourself, you might find it easier to use the cwd command from the Perl Cwd module to print the current directory path. You should also take care that when you finish searching the current directory that you chdir back to the parent directory or your recursive routine will not produce the desired results. For example, when you finish searching /Users/bvz/animation you need to restore the directory to /Users/bvz.