Minimal Perl For UNIX and Linux People
BY
TIM MAHER
MANNING Greenwich (74° w. long.)
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. Cherokee Station PO Box 20386 New York, NY 10021
Fax: (609) 877-8256 email:
[email protected]
©2007 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Manning Publications Co. 209 Bruce Park Avenue Greenwich, CT 06830
Copyeditor: Tiffany Taylor Typesetters: Denis Dalinnik, Dottie Marsico Cover designer: Leslie Haimes
ISBN 1-932394-50-8 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – VHG – 10 09 08 07 06
To Yeshe Dolma Sherpa, whose fortitude, endurance, and many sacrifices made this book possible. To my parents, Gloria Grady Washington and William N. Maher, who indulged my early interests in literature. To my limbic system, with gratitude for all the good times we’ve had together.
brief contents Part 1
Minimal Perl: for UNIX and Linux Users 1 1 Introducing Minimal Perl 3 2 Perl essentials 16 3 Perl as a (better) grep command 53 4 Perl as a (better) sed command 89 5 Perl as a (better) awk command 121 6 Perl as a (better) find command 178
Part 2
Minimal Perl: for UNIX and Linux Shell Programmers 203 7 Built-in functions 205 8 Scripting techniques 247 9 List variables 295 10 Looping facilities
330
11 Subroutines and variable scoping 362 12 Modules and the CPAN 388
vii
contents foreword xvii preface xix acknowledgments xxii about this book xxiii about the cover illustration xxxiv list of tables xxxv
Part 1
Minimal Perl: for UNIX and Linux Users 1 1 Introducing Minimal Perl 1.1 A visit to Perlistan
3
3
Sometimes you need a professional guide
5
1.2 Perl can be simple 7 1.3 A