Exercises
See Appendix A for answers to the following exercises:
[7] Make a pattern that will match three consecutive copies of whatever is currently contained in
$what
. That is, if$what
isfred
, your pattern should matchfredfredfred
. If$what
isfred|barney
, your pattern should matchfredfredbarney
,barneyfredfred
,barneybarneybarney
, or many other variations. (Hint: You should set$what
at the top of the pattern test program with a statement likemy $what = 'fred|barney';
.)[12] Write a program that makes a modified copy of a text file. In the copy, every string
Fred
(case insensitive) should be replaced withLarry
. (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
.[8] Modify the previous program to change every
Fred
toWilma
and everyWilma
toFred
. Now input likefred&wilma
should look likeWilma&Fred
in the output.[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.
[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.