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 ...

Get Learning Perl, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.