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

  1. (10 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. (20 points) Write a Perl program named center.pl 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:

    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. (30 points) Write a perl script named backup.pl 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~
         
  4. (30 points) Write a perl script named count_bytes.pl that takes a directory name as a command-line argument and prints the size of each .pl file in both that directory and recursively in any sub-directories. For example: UNIX> perl count_bytes /Users/bvz /Users/bvz/rewrite.pl: 704 /Users/bvz/animation/lighten.pl: 5888 /Users/bvz/animation/darken.pl: 1192 /Users/bvz/cs594/notes/example.pl: 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.