Exercises

See Appendix A for answers to the following exercises.

Several of these exercises ask you to use the test program from this chapter. You could manually type this program, taking great care to get all of the odd punctuation marks correct.[212] But you’ll probably find it faster and easier to download the program and some other goodies from the O’Reilly web-site, as we mentioned in the Preface. You’ll find this program under the name pattern_test.[213]

  1. [8] Using the pattern test program, make a pattern to match the string match. Try the program with the input string beforematchafter. Does the output show the three parts of the match in the right order?

  2. [7] Using the pattern test program, make a pattern that matches if any word (in the \w sense of word) ends with the letter a. Does it match wilma but not barney? Does it match Mrs. Wilma Flintstone? What about wilma&fred? Try it on the sample text file from the previous chapter’s exercises (and add these test strings if they weren’t already in there).

  3. [5] Modify the program from the previous exercise so the word ending with the letter a is captured into memory $1. Update the code to display that variable’s contents in single quotes, something like $1 contains 'Wilma'.

  4. [5] Extra credit exercise: Modify the program from the previous exercise so that immediately following the word ending in a it will capture up to five characters (if there are that many characters) in a separate memory variable. Update the code to display both memory ...

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.