Foy - Mastering Perl (O'Reilly, 2007).pdf

(1390 KB) Pobierz
435590690 UNPDF
Mastering Perl
brian d foy
foreword by Randal L. Schwartz
Beijing Cambridge Farnham Köln Paris Sebastopol Taipei Tokyo
435590690.023.png 435590690.024.png 435590690.025.png 435590690.026.png 435590690.001.png 435590690.002.png
Mastering Perl
by brian d foy
Copyright © 2007 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles ( http://safari.oreilly.com ). For more information, contact our corporate/
institutional sales department: (800) 998-9938 or corporate@oreilly.com .
Editor: Andy Oram
Production Editor: Adam Witwer
Proofreader: Sohaila Abdulali
Indexer: Joe Wizda
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrators: Robert Romano and Jessamyn Read
Printing History:
July 2007:
First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Mastering Perl , the image of a vicuña mother and her young, and related trade dress
are trademarks of O’Reilly Media, Inc.
Many of the designations uses by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly 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 author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
TM
This book uses RepKover™, a durable and flexible lay-flat binding.
ISBN-10: 0-596-52724-1
ISBN-13: 978-0-596-52724-2
[M]
435590690.003.png 435590690.004.png 435590690.005.png 435590690.006.png 435590690.007.png
 
Table of Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
1.
Introduction: Becoming a Master . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What It Means to Be a Master
2
Who Should Read This Book
3
How to Read This Book
3
What Should You Know Already?
4
What I Cover
4
What I Don’t Cover
5
2.
Advanced Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
References to Regular Expressions
7
Noncapturing Grouping, (?:PATTERN)
13
Readable Regexes, /x and (?#...)
14
Global Matching
15
Lookarounds
19
Deciphering Regular Expressions
25
Final Thoughts
28
Summary
29
Further Reading
29
3.
Secure Programming Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Bad Data Can Ruin Your Day
31
Taint Checking
32
Untainting Data
38
List Forms of system and exec
42
Summary
44
Further Reading
44
v
435590690.008.png 435590690.009.png 435590690.010.png 435590690.011.png 435590690.012.png
 
4.
Debugging Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Before You Waste Too Much Time
47
The Best Debugger in the World
48
perl5db.pl
59
Alternative Debuggers
60
Other Debuggers
64
Summary
66
Further Reading
66
5.
Profiling Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Finding the Culprit
69
The General Approach
73
Profiling DBI
74
Devel::DProf
83
Writing My Own Profiler
85
Profiling Test Suites
86
Summary
88
Further Reading
88
6.
Benchmarking Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Benchmarking Theory
91
Benchmarking Time
93
Comparing Code
96
Don’t Turn Off Your Thinking Cap
97
Memory Use
102
The perlbench Tool
107
Summary
109
Further Reading
110
7.
Cleaning Up Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Good Style
111
perltidy
112
De-Obfuscation
114
Perl::Critic
118
Summary
123
Further Reading
123
8.
Symbol Tables and Typeglobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Package and Lexical Variables
125
The Symbol Table
128
Summary
136
Further Reading
136
vi | Table of Contents
435590690.013.png 435590690.014.png 435590690.015.png 435590690.016.png 435590690.017.png
 
9.
Dynamic Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Subroutines As Data
137
Creating and Replacing Named Subroutines
141
Symbolic References
143
Iterating Through Subroutine Lists
145
Processing Pipelines
147
Method Lists
147
Subroutines As Arguments
148
Autoloaded Methods
152
Hashes As Objects
154
AutoSplit
154
Summary
155
Further Reading
155
10. Modifying and Jury-Rigging Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Choosing the Right Solution
157
Replacing Module Parts
160
Subclassing
162
Wrapping Subroutines
167
Summary
169
Further Reading
170
11. Configuring Perl Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Things Not to Do
171
Better Ways
174
Command-Line Switches
177
Configuration Files
183
Scripts with a Different Name
187
Interactive and Noninteractive Programs
188
perl’s Config
189
Summary
191
Further Reading
191
12. Detecting and Reporting Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Perl Error Basics
193
Reporting Module Errors
199
Exceptions
202
Summary
209
Further Reading
209
13. Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Recording Errors and Other Information
211
Table of Contents | vii
435590690.018.png 435590690.019.png 435590690.020.png 435590690.021.png 435590690.022.png
 
Zgłoś jeśli naruszono regulamin