Student Workbook for Learning Perl
brian d foy
Copyright 2012 brian d foy
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (.
Nutshell Handbook, the Nutshell Handbook logo, and the OReilly logo are registered trademarks of OReilly Media, Inc. Student Workbook for Learning Perl, Second Edition and related trade dress are trademarks of OReilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and OReilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
O'Reilly Media
Preface
This workbook is an additional set of exercises to supplement those already in Learning Perl, Sixth Edition . The chapter and page references apply only to that edition. Additionally, unless I denote otherwise, Learning Perl without qualification means the sixth edition. That book already contains some exercises, and I try to cover the topics those exercises didnt.
Ive been teaching Perl since 1998, and my beginner classes use Learning Perl as the course text. Along the way, Ive posed a lot of additional problems to students so they could test their understanding of the topics I covered and some of the additional information I added in the lecture. In doing this, I pose two sorts of problems: one of simple knowledge where knowing the right fact or trick makes the problem easy, and the other somewhat clever where a Perl implementation of a particular technique solves the problem. Ive tried to include both of those sorts of problems in this book.
You should find some exercises are easy, some require that you find nuggets of information you may have missed on a first reading of Learning Perl , and others require you know something about algorithms or programming that arent simply a question of Perl knowledge and might require you to do some research on your own. As you learn about how to learn about Perl, whether through its documentation or online resources, I pose these challenging problems expecting that youll use everything available to you. Theres a lot more to programming than just the syntax.
This isnt a classroom and you arent being graded, unless it is a classroom and you are being graded. I wont think you are cheating if you use Google, and you shouldnt feel that you have to come up with everything just by staring at a blank document. If someone is grading you, just show them this paragraph and tell them I say its okay. If youre the instructor, fear not: no one reads the preface anyway, especially if its not on the test.
You should be able to complete most of the exercises with Perl 5.8, and I expect that you might even be able to use Perl 5.004, a quite ancient version indeed. Some of the exercises require a minimum version of Perl because I want you to use a particular feature introduced in that version, and Ill note the minimum version in the exercise when its appropriate. If you dont note a minimum version assume that its at least Perl 5.8. Id prefer that you use a supported version of Perl, which at this writing is at least Perl 5.14. However, I know that some of you dont get to make that choice.
Perls motto, for good or bad, is Theres More Than One Way To Do It, so dont take my solutions as gospel. Remember that the corollary to that is But most of them are wrong, so dont go out of your way to avoid techniques that I show. I show you a way to do it, and often I give you more than one way to do it. However, if you accomplished the task and your solution doesnt look remotely similar to mine, thats okay (probably).
Some of my answers have extra material that show techniques youll read about in later chapters. I dont expect you to use those techniques, but I show them because they are the techniques and features youll use in everyday programming once you finish the book. They are just a taste of things to come.
Part I. Exercises
Introduction
Scalar Data
Lists and Arrays
Subroutines
Input and Output
Hashes
In the World of Regular Expressions
Matching with Regular Expressions
Processing Text with Regular Expressions
More Control Structures
Perl Modules
File Tests
Directory Operations
Strings and Sorting
Smart Matching and given-when
Process Management
Some Advanced Perl Techniques
Databases (bonus chapter)
Chapter 1. Introduction
1.1. The perldoc command is the key to all of the Perl documentation, and perl comes with literally thousands of pages of printed documentation (should you print them all out, which would take quite a long time). Take yourself on a tour of the perldoc command starting with the perl documentation. Next, try the perlfunc documentation, which lists all of the built-in Perl functions. This information may also be available in other formats on your system, and on some systems, you may need to download a separate package to get the Perl documentation (which should be a crime, but is becoming more common):
$ perldoc perl$ perldoc perlfunc
()
)
)
)
)
Chapter 2. Scalar Data
2.1. In Perl versions 5.6 onward, you can also turn on warnings with a pragma instead of the w command-line switch:
use warnings;
You may already have a program that outputs warnings, but if you dont, write one and add the warnings pragma to it. If you cant come up with one, try these programs, each of which outputs a different sort of warning. Which warning do you get for each?
Program 1:
print;
Program 2:
3 + 4;
Program 3:
print $n + 1;
()
instead by replacing the warnings line with this one:
use diagnostics;
What difference do you see? ()
)
)
2.5. Remembering that Perl operations of the same precedence use associativity to decide what happens first, what is the answer to these operations? Which operation happens first? Does it matter?
2 ** 3 ** 42 / 3 * 42 + 3 * 4 ** 5 - 6
()
)
)
2.8. Write a program that prompts the user for a code point and prints the character for that code point. If you cant think of any interesting code points, try U+26F3, U+267A, U+2126, and U+03B6. Which characters are those code points?
If you need help setting up your terminal to handle Unicode, see Appendix C in Learning Perl .
()
Chapter 3. Lists and Arrays
)
)
)
)
)
)
Chapter 4. Subroutines
In this chapter in Learning Perl , we introduced the strict
pragma that enforces some good programming practices. For the rest of this workbook, Im going to make strict-clean programs and Ill use the strict
pragma at the top of my programs.