- (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.
- (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>.
with:
<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:
- s: wrap the search past newline characters
- m: let the ^ match expressions starting after a /n character in a string
- g: perform multiple substitutions
- (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.
- Web page: Create an html file named
icecream.html that 1) welcomes the viewer to
"Brad's Fantastic Ice Cream", 2) provides type in text boxes for
the street address and person's phone number, and 3) provides
check boxes for Brad's fine flavors, which will be
Vanilla, Chocolate, Strawberry, Mocha Chip, and Coffee
Heath Bar Crunch. The choices should not be mutually
exclusive because Brad realizes that one might want multiple
scoops. When the user presses the "Submit" button your
Perl program should be called. Use GET as your submission
method.
- Perl program: Create a Perl program named icecream.cgi
that takes the information
provided by the web page and creates a dynamic web page
with the following information:
- Street address and phone number
- The requested flavors. If Brad's does not provide that
flavor then scold the user for trying to spoof your
program by bypassing the html page.
- The cost of the flavors before tax: Each scoop costs
$1.25.
- The sales tax: The sales tax is 9.25%. Only two decimal
points should be displayed.
- The total cost: The total cost is the sum of the cost of
the flavors and the sales tax. Only two decimal points
should be displayed.
Here is some useful information for both this problem and the following problem:
- Here are some snapshots of what your pages might look like.
- Here are some frequent errors that will cause the brower to
tell you that you can't run your cgi script.
- Here are some detailed directions to use for storing and testing your
Perl .cgi scripts:
- Create a cgi-bin subdirectory in your www-home directory
- Put your perl .cgi scripts in the cgi-bin directory. Make sure they end with the extension .cgi rather than .pl.
- 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.
- The URL for reaching your scripts should be "http://web.eecs.utk.edu/~your_user_id/cgi-bin/filename.cgi". You will put this URL in the action attribute of your form element.
- You may also need to access your initial web page (i.e., icecream.html), by typing in web.eecs.utk.edu/ and the path to your icecream.html file.
- (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:
- 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:
- select between MasterCard, Visa, and American Express. Make MasterCard
be the default selection.
- 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.
- 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.
- submit the form.
- Copy your icecream.html file from problem 3, rename it
icecream_h.html, and make its action attribute call icecream_h.cgi.
- 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.
- 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.
- (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~
- (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.