Learning to Program in Perl
by Graham J Ellis
Languages of the Web
Learning to Program in Perl version 1.7
Written by Graham Ellis [email protected]
Design by Lisa Ellis Well House Consultants, Ltd. 404, The Spa, Melksham, Wiltshire SN12 6QL England
+44 (0) 1225 708 225 (phone) +44 (0) 1225 707 126 (fax)
Find us on the World Wide Web at: http://www.wellho.net
Or contact us at: [email protected]
Copyright © 2003 by Well House Consultants, Ltd. Printed in Great Britain.
Printing History
- May 1999
- 1.0
1.1 1.2 1.3 1.4
First Edition
February 2000 June 2000
Minor additions Compliation of modules Name change, revisions Added modules Added modules Updated modules Updated modules
October 2000 April 2002 September 2002 1.5 January 2003 February 2003
1.6 1.7
This manual was printed on 21 May 2003.
Notice of Rights
All rights reserved. No part of this manual, including interior design, may be reproduced or translated into any language in any form, or transmitted in any form or by any means electronic, mechanical, photocopying, recording or otherwise, without prior written permission of Well House Consultants except in the case of brief quotations embodied in critical articles and reviews. For more information on getting permission for reprints and excerpts, contact Graham Ellis at Well House Consultants.
This manual is subject to the condition that it shall not, by way of trade or otherwise, be lent, sold, hired out or otherwise circulated without the publisher's prior consent, incomplete nor in any form of binding or cover other than in which it is published and without a similar condition including this condition being imposed on the subsequent receiver.
Notice of Liability
All brand names and product names used in this book are trade names, service marks, trademarks or registered trademarks of their respective owners. Well House Consultants is not associated with any product or vendor in this book and where such designations appear in this book, and Well House Consultants was aware of a trademark claim, the designations have been appropriately capitalised. Well House Consultants cannot attest to the accuracy of this information and use of a term should not be regarded as affecting the validity of any trademark or service mark.
The information in this book is distributed on an "as is" basis, without warranty. Whilst every precaution has been taken in the preparation of this manual, neither the author nor Well House Consultants assumes responsibility for errors or omissions, nor shall be liable to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by the computer software and hardware products described herein.
Table of Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1 What is Perl? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.2 Perl Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.3 Perl Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.4 Examples of Perl in use . . . . . . . . . . . . . . . . . . . . 15
2 Hello Perl World. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.1 Let's study a first program. . . . . . . . . . . . . . . . . . . 17 2.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3 Variables and Operations . . . . . . . . . . . . . . . . . . . . . . . . 23
3.1 Reading from the user . . . . . . . . . . . . . . . . . . . . . 23 3.2 More about variables . . . . . . . . . . . . . . . . . . . . . . 24 3.3 How do I do calculations?. . . . . . . . . . . . . . . . . . . 27 3.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4 Perl Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.1 First Perl program. . . . . . . . . . . . . . . . . . . . . . . . . 31 4.2 Comments and documentation. . . . . . . . . . . . . . . 32 4.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
4.4 Reading data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
5 More about the Perl Environment . . . . . . . . . . . . . . . . . 43
5.1 Integrating your program with your computer. . . . 43 5.2 Unix and Linux systems . . . . . . . . . . . . . . . . . . . . 43 5.3 Windows 98, 2000 and Windows NT systems . . . 45 5.4 MS-DOS users . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 5.5 Macintosh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 5.6 The compiler and the interpreter. . . . . . . . . . . . . . 47 5.7 Some questions on compilers and interpreters . . 48 5.8 Debugging tools . . . . . . . . . . . . . . . . . . . . . . . . . . 49 5.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
6 Conditionals and Loops . . . . . . . . . . . . . . . . . . . . . . . . . 53
6.1 The "if" statement . . . . . . . . . . . . . . . . . . . . . . . . . 53 6.2 The "while" statement. . . . . . . . . . . . . . . . . . . . . . 54 6.3 Shorthand operators. . . . . . . . . . . . . . . . . . . . . . . 56 6.4 Ways of writing numbers . . . . . . . . . . . . . . . . . . . 59 6.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
- Learning to Program in Perl
- 3
Contents
7 Analysing a Programming Task. . . . . . . . . . . . . . . . . . . 61
7.1 A small job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
7.2 As a job gets larger. . . . . . . . . . . . . . . . . . . . . . . . 67 7.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
8 Initial String Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
8.1 String handling functions . . . . . . . . . . . . . . . . . . . 73 8.2 String handling operators . . . . . . . . . . . . . . . . . . . 74 8.3 Comparing strings exactly . . . . . . . . . . . . . . . . . . 76 8.4 Comparing strings to regular expressions . . . . . . 77 8.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
9 More Loops and Conditionals . . . . . . . . . . . . . . . . . . . . 85
9.1 The variety that is Perl . . . . . . . . . . . . . . . . . . . . . 85 9.2 More conditional statements. . . . . . . . . . . . . . . . . 85 9.3 More loop statements . . . . . . . . . . . . . . . . . . . . . . 89 9.4 Breaking a loop. . . . . . . . . . . . . . . . . . . . . . . . . . . 90 9.5 Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 9.6 The goto statement. . . . . . . . . . . . . . . . . . . . . . . . 92 9.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
10 File Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
10.1 File input and output . . . . . . . . . . . . . . . . . . . . . . . 95 10.2 File testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 10.3 Formatted printing. . . . . . . . . . . . . . . . . . . . . . . . 100 10.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
11 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
11.1 Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 11.2 The length of a list . . . . . . . . . . . . . . . . . . . . . . . 107 11.3 Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 11.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
11.5 Functions that operate on lists . . . . . . . . . . . . . . 112 11.6 Iterating through a list . . . . . . . . . . . . . . . . . . . . . 114 11.7 List slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 11.8 Anonymous lists . . . . . . . . . . . . . . . . . . . . . . . . . 117 11.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
12 Subroutines in Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
12.1 What are subroutines and why? . . . . . . . . . . . . . 119 12.2 Calling a subroutine . . . . . . . . . . . . . . . . . . . . . . 121 12.3 Writing your own subroutine . . . . . . . . . . . . . . . . 123 12.4 Writing subroutines in a separate file . . . . . . . . . 125 12.5 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
12.6 packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 12.7 Calling objects . . . . . . . . . . . . . . . . . . . . . . . . . . 134
- 4
- Well House Consultants
Contents
12.8 Writing a class – an introduction. . . . . . . . . . . . . 136
13 Special Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
13.1 The Command line . . . . . . . . . . . . . . . . . . . . . . . 137 13.2 Information variables . . . . . . . . . . . . . . . . . . . . . 138 13.3 Behaviour changing variables. . . . . . . . . . . . . . . 139 13.4 The default input and pattern match space . . . . 141 13.5 More options on Perl's command line. . . . . . . . . 143 13.6 Others. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 13.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
14 Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
14.1 Setting up a hash . . . . . . . . . . . . . . . . . . . . . . . . 145 14.2 Accessing a hash . . . . . . . . . . . . . . . . . . . . . . . . 146 14.3 Processing every element of a hash. . . . . . . . . . 147 14.4 Ordering a hash (sorting) . . . . . . . . . . . . . . . . . . 149 14.5 Programming techniques . . . . . . . . . . . . . . . . . . 153 14.6 Special hashes . . . . . . . . . . . . . . . . . . . . . . . . . . 156 14.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
15 More on Character Strings . . . . . . . . . . . . . . . . . . . . . . 161
15.1 Summary to date . . . . . . . . . . . . . . . . . . . . . . . . 161 15.2 Extracting information from a match. . . . . . . . . . 162 15.3 More about regular expressions . . . . . . . . . . . . . 164 15.4 Match modifiers . . . . . . . . . . . . . . . . . . . . . . . . . 166 15.5 Alternative delimiters . . . . . . . . . . . . . . . . . . . . . 168 15.6 Some favourite regular expressions . . . . . . . . . . 168
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
15.7 Substitutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 15.8 Regular expression efficiency. . . . . . . . . . . . . . . 175 15.9 tr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 15.10 Handling binary text . . . . . . . . . . . . . . . . . . . . . . 176 15.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
16 HTML – Quick Reminder . . . . . . . . . . . . . . . . . . . . . . . . 181
16.1 Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 16.2 Structure of a page . . . . . . . . . . . . . . . . . . . . . . . 181 16.3 Special Characters and new lines . . . . . . . . . . . 181 16.4 Some common tags . . . . . . . . . . . . . . . . . . . . . . 182 16.5 Lists, Tables, Frames, Forms, Images etc . . . . . 183 16.6 Which HTML standard? . . . . . . . . . . . . . . . . . . . 184
17 Perl on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
17.1 The HTML form. . . . . . . . . . . . . . . . . . . . . . . . . . 185 17.2 Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 17.3 Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 17.4 All together!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 17.5 The power of using Perl on the Web . . . . . . . . . 188 17.6 A real example of Perl on the Web. . . . . . . . . . . 188 17.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
- Learning to Program in Perll
- 5
Contents
18 System Dependencies. . . . . . . . . . . . . . . . . . . . . . . . . . 197
18.1 The Philosophy. . . . . . . . . . . . . . . . . . . . . . . . . . 197 18.2 Shell access . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 18.3 System database enquiries . . . . . . . . . . . . . . . . 200 18.4 How Perl helps on crossplatform requirements . 201 18.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
19 More than Simple Lists and Hashes!. . . . . . . . . . . . . . 205
19.1 Multidimensional arrays . . . . . . . . . . . . . . . . . . . 205 19.2 Something more complex. . . . . . . . . . . . . . . . . . 207 19.3 Grouping in Perl . . . . . . . . . . . . . . . . . . . . . . . . . 209 19.4 Interpreting a complex reference to a variable . . 210 19.5 Design MATTERS . . . . . . . . . . . . . . . . . . . . . . . 211 19.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
20 Handling Dates and Time . . . . . . . . . . . . . . . . . . . . . . . 213
20.1 So far . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 20.2 How Perl handles dates and times . . . . . . . . . . . 214 20.3 Convertors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 20.4 Handling centuries . . . . . . . . . . . . . . . . . . . . . . . 217 20.5 Elapsed time sleep . . . . . . . . . . . . . . . . . . . . . . . 217 20.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
21 Practical Example – Perl in use . . . . . . . . . . . . . . . . . . 221
21.1 The requirement . . . . . . . . . . . . . . . . . . . . . . . . . 221 21.2 Plain Old Documentation (POD). . . . . . . . . . . . . 231 21.3 Possible enhancements . . . . . . . . . . . . . . . . . . . 231
22 Libraries and Resources. . . . . . . . . . . . . . . . . . . . . . . . 233
22.1 Standard Perl modules . . . . . . . . . . . . . . . . . . . . 233 22.2 The CPAN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 22.3 Utility programs. . . . . . . . . . . . . . . . . . . . . . . . . . 237 22.4 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . 238 22.5 Web resources . . . . . . . . . . . . . . . . . . . . . . . . . . 239 22.6 Newsgroups . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 22.7 Chat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 22.8 Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 22.9 Meeting users, getting local support and training 242
23 Perl 6 Look Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
23.1 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 23.2 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 23.3 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 23.4 Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 23.5 Conditionals and loops . . . . . . . . . . . . . . . . . . . . 248 23.6 Exception handling . . . . . . . . . . . . . . . . . . . . . . . 250 23.7 Rules and grammar . . . . . . . . . . . . . . . . . . . . . . 251 23.8 Under the bonnet . . . . . . . . . . . . . . . . . . . . . . . . 253 23.9 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
24 A Quick Look Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
24.1 Fundamental and advanced topics. . . . . . . . . . . 255
- 6
- Well House Consultants
Contents
24.2 Other facilities in the Perl language . . . . . . . . . . 256 24.3 Other facilities in Perl – further modules. . . . . . . 260 24.4 Perl in other guises. . . . . . . . . . . . . . . . . . . . . . . 263 24.5 And also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Appendix. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Exercise sample answers . . . . . . . . . . . . . . . . . 265 CPAN sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Common Errors in Perl . . . . . . . . . . . . . . . . . . . 273
WHC Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281