Section 9.6. Exercises

9.6. Exercises

See Appendix A for answers to the following exercises:

  1. [7] Make a pattern that will match three consecutive copies of whatever is currently contained in $what. That is, if $what is fred, your pattern should match fredfredfred. If $what is fred|barney, your pattern should match fredfredbarney, barneyfredfred, barneybarneybarney, or many other variations. (Hint: You should set $what at the top of the pattern test program with a statement like my $what = 'fred|barney';.)

  2. [12] Write a program that makes a modified copy of a text file. In the copy, every string Fred (case insensitive) should be replaced with Larry. (So, "Manfred Mann" should become "ManLarry Mann".) The input filename should be given on the command line (don't ask the user), and the output file name should be the corresponding file name ending with .out.

  3. [8] Modify the previous program to change every Fred to Wilma and every Wilma to Fred. Now input like fred&wilma should look like Wilma&Fred in the output.

  4. [10] Extra credit exercise: Write a program to add a copyright line to all of your exercise answers so far, by placing a line such as:

         ## Copyright (C) 20XX by Yours Truly

    Place it in the file immediately after the "shebang" line. You should edit the files "in place" and keep a backup. Presume that the program will be invoked with the filenames to edit on the command line.

  5. [15] Extra extra credit exercise: Modify the previous program so it doesn't edit the files that contain the copyright line. Hint: You might need to know that the name of the file being read by the diamond operator is in $ARGV.

Learning Perl
Learning Perl, 5th Edition
ISBN: 0596520107
EAN: 2147483647
Year: 2003
Pages: 232

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: