Learning Perl
by Randal L. Schwartz , brian d foy , and Tom Phoenix
Copyright 2017 Randal L. Schwartz, brian foy, and Tom Phoenix. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editor: Heather Scherer
- Production Editor: Melanie Yarbrough
- Copyeditor: Jasmine Kwityn
- Proofreader: Sonia Saruba
- Indexer: Lucie Haskins
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Rebecca Demarest
- October 2016: Seventh Edition
Revision History for the Seventh Edition
- 2016-10-05: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491954324 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Learning Perl, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-95432-4
[LSI]
Preface
Welcome to the seventh edition of Learning Perl, updated for Perl 5.24 and its latest features. This book is still mostly good even if you are still using Perl 5.8 (although, its been a long time since it was released; have you thought about upgrading?).
We hope youre reading this preface before you buy the book because theres a historical hiccup that may cause some confusion. Theres another language, Perl 6, that started off as a replacement for Perl 5 but then went out on its own without changing the name. As we write this, Perl 5 is probably the version you want. Its the widely installed and used language that people mean when they say simply Perl. Its the one you want if you dont know why this paragraph is here.
If youre looking for the best way to spend your first 30 to 45 hours with the Perl programming language, youve found it. In the pages that follow, youll find a carefully paced introduction to the language that is the workhorse of the Internet, as well as the language of choice for system administrators, web hackers, and casual programmers around the world. Weve designed this book based on the in-person classes we teach, so weve timed the book for a weeks worth of work.
We cant give you all of Perl in just a few hours. The books that promise that are probably fibbing a bit. Instead, weve carefully selected a useful subset of Perl for you to learn, good for programs from 1 to 128 lines long, which end up being about 90% of the programs in use out there. And when youre ready to go on, you can get Intermediate Perl, which picks up where this book leaves off. Weve also included a number of pointers for further education.
Each chapter is small enough so you can read it in an hour or two. Each chapter ends with a series of exercises to help you practice what youve just learned, with the answers in for your reference. Thus, this book is ideally suited for a classroom Introduction to Perl course. We know this directly because the material for this book was lifted almost word for word from our flagship Learning Perl course, delivered to thousands of students around the world. However, weve designed the book for self-study as well.
Perl lives as the toolbox for Unix, but you dont have to be a Unix guru, or even a Unix user, to read this book. Unless otherwise noted, everything were saying applies equally well to Windows ActivePerl from ActiveState and Strawberry Perl and pretty much every other modern implementation of Perl.
Although you dont need to know a single thing about Perl to begin reading this book, we recommend that you already have familiarity with basic programming concepts such as variables, loops, subroutines, and arrays, and the all-important editing a source code file with your favorite text editor. We dont spend any time trying to explain those concepts. Although were pleased that weve had many reports of people successfully picking up Learning Perl and grasping Perl as their first programming language, of course we cant promise the same results for everyone.
Typographical Conventions
The following font conventions are used in this book:
Constant width
Used for method names, function names, variables, and attributes. It is also used for code examples.
Constant width bold
Used to indicate user input.
Constant width italic
Used to indicate a replaceable item in code (e.g., filename, where you are supposed to substitute an actual filename).
ItalicUsed for filenames, URLs, hostnames, commands in text, important words on first mention, and emphasis.
[37]At the start of an exercises text, we provide a (very rough) estimate of how many minutes you can expect to spend on that particular exercise.
Code Examples
This book is here to help you get your job done. You are invited to copy the code in the book and adapt it for your own needs. Rather than copying by hand, however, we encourage you to download the code from the books companion website.
In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from OReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, authors, publisher, and ISBN. For example: Learning Perl, 7th edition, by Randal Schwartz, brian foy, and Tom Phoenix (OReilly). Copyright 2017 Randal Schwartz, brian foy, and Tom Phoenix, 978-1-491-95432-4. If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .
Safari Books Online
Note
Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the worlds leading authors in technology and business.
Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.
Safari Books Online offers a range of plans and pricing for enterprise, government, and education, and individuals.
Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like OReilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.