Book HomeLearning Perl, 3rd EditionSearch this book

9.9. Exercises

See Section A.8, "Answers to Chapter 9 Exercises" 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 or barneyfredfred or 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. [15] Write a program that looks through the perlfunc.pod file for lines that start with =item and 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 the perlfunc.pod file on your system with the command perldoc -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 print wilma. For the second, it should print fred (ignoring the word flintstone, since we're interested only in the identifier name):

    =item wilma 
    =item fred flintstone
  3. [10] Modify the previous program to list only the identifier names that appear more than twice on those =item lines, and tell how many times each one appeared. (That is, we want to know which identifier names appear on at least three separate =item lines in the file.) There should be a couple of dozen, depending upon your version of Perl.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.