Exercises
See , "Answers to Chapter 9 Exercises" 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$whatisfred, your pattern should matchfredfredfred. If$whatisfred|barney, your pattern should matchfredfredbarneyorbarneyfredfredorbarneybarneybarneyor many other variations. (Hint: You should set$whatat the top of the pattern test program with a statement likemy $what = 'fred|barney';.) - [15] Write a program that looks through the
perlfunc.podfile for lines that start with=itemand some whitespace, followed by a Perl identifier name (made of letters, digits, and underscores, but never starting with a digit), like the lines below. (There may be more text on the line after the identifier name; just ignore it.) You can locate theperlfunc.podfile on your system with the commandperldoc -l perlfunc, or ask your local expert. (Hint: You'll need the diamond operator to open this file. How will it get the filename?) Have the program print each identifier name as it finds it; there will be hundreds of them, and many will appear more than once in the file.As an example, the following lines of input resemble what you'll find in
perlfunc.pod. For the first line, the program should printwilma. For the second, it should printfred(ignoring the wordflintstone, since we're interested only in the identifier name):=item wilma =item fred flintstone
- [10] Modify the previous program to list only the identifier names that appear more than twice on those
=itemlines, and tell how many times each one appeared. (That is, we want to know which identifier names appear on at least three separate=itemlines in the file.) There should be a couple of dozen, depending upon your version of Perl.