
<p><strong>Learning to Program in Perl </strong></p><p>by Graham J Ellis </p><p>Languages of the Web </p><p><strong>Learning to Program in Perl </strong><sup style="top: 0em;">version 1.7 </sup></p><p>Written by Graham Ellis <a href="mailto:[email protected]" target="_blank"><em>[email protected] </em></a></p><p>Design by Lisa Ellis Well House Consultants, Ltd. 404, The Spa, Melksham, Wiltshire SN12 6QL England </p><p>+44 (0) 1225 708 225 (phone) +44 (0) 1225 707 126 (fax) </p><p>Find us on the World Wide Web at: <a href="/goto?url=http://www.wellho.net" target="_blank"><em>http://www.wellho.net </em></a></p><p>Or contact us at: <a href="mailto:[email protected]" target="_blank"><em>[email protected] </em></a></p><p>Copyright © 2003 by Well House Consultants, Ltd. Printed in Great Britain. </p><p><strong>Printing History </strong></p><p></p><ul style="display: flex;"><li style="flex:1">May 1999 </li><li style="flex:1">1.0 </li></ul><p>1.1 1.2 1.3 1.4 <br>First Edition <br>February 2000 June 2000 <br>Minor additions Compliation of modules Name change, revisions Added modules Added modules Updated modules Updated modules <br>October 2000 April 2002 September 2002 1.5 January 2003 February 2003 <br>1.6 1.7 <br>This manual was printed on 21 May 2003. </p><p><strong>Notice of Rights </strong></p><p>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. </p><p>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. </p><p><strong>Notice of Liability </strong></p><p>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. </p><p>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. </p><p><strong>Table of Contents </strong></p><p><strong>1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 </strong></p><p>1.1 What is Perl? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.2 Perl Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.3 Perl Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.4 Examples of Perl in use . . . . . . . . . . . . . . . . . . . . 15 </p><p><strong>2 Hello Perl World. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 </strong></p><p>2.1 Let's study a first program. . . . . . . . . . . . . . . . . . . 17 2.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 </em></p><p><strong>3 Variables and Operations . . . . . . . . . . . . . . . . . . . . . . . . 23 </strong></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 </em></p><p><strong>4 Perl Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 </strong></p><p>4.1 First Perl program. . . . . . . . . . . . . . . . . . . . . . . . . 31 4.2 Comments and documentation. . . . . . . . . . . . . . . 32 4.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 </em></p><p>4.4 Reading data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 </em></p><p><strong>5 More about the Perl Environment . . . . . . . . . . . . . . . . . 43 </strong></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 </em></p><p><strong>6 Conditionals and Loops . . . . . . . . . . . . . . . . . . . . . . . . . 53 </strong></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 </em></p><p></p><ul style="display: flex;"><li style="flex:1">Learning to Program in Perl </li><li style="flex:1">3</li></ul><p>Contents </p><p><strong>7 Analysing a Programming Task. . . . . . . . . . . . . . . . . . . 61 </strong></p><p>7.1 A small job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 </em></p><p>7.2 As a job gets larger. . . . . . . . . . . . . . . . . . . . . . . . 67 7.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 </em></p><p><strong>8 Initial String Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 </strong></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 </em></p><p><strong>9 More Loops and Conditionals . . . . . . . . . . . . . . . . . . . . 85 </strong></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 </em></p><p><strong>10 File Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 </strong></p><p>10.1 File input and output . . . . . . . . . . . . . . . . . . . . . . . 95 10.2 File testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 10.3 Formatted printing. . . . . . . . . . . . . . . . . . . . . . . . 100 10.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 </em></p><p><strong>11 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 </strong></p><p>11.1 Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 11.2 The length of a list . . . . . . . . . . . . . . . . . . . . . . . 107 11.3 Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 11.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 </em></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 </em></p><p><strong>12 Subroutines in Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 </strong></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 </em></p><p>12.6 packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 12.7 Calling objects . . . . . . . . . . . . . . . . . . . . . . . . . . 134 </p><p></p><ul style="display: flex;"><li style="flex:1">4</li><li style="flex:1">Well House Consultants </li></ul><p>Contents </p><p>12.8 Writing a class – an introduction. . . . . . . . . . . . . 136 </p><p><strong>13 Special Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 </strong></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 </em></p><p><strong>14 Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 </strong></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 </em></p><p><strong>15 More on Character Strings . . . . . . . . . . . . . . . . . . . . . . 161 </strong></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 </em></p><p>15.7 Substitutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 15.8 Regular expression efficiency. . . . . . . . . . . . . . . 175 15.9 tr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 15.10 Handling binary text . . . . . . . . . . . . . . . . . . . . . . 176 15.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 </em></p><p><strong>16 HTML – Quick Reminder . . . . . . . . . . . . . . . . . . . . . . . . 181 </strong></p><p>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 </p><p><strong>17 Perl on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 </strong></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 </em></p><p></p><ul style="display: flex;"><li style="flex:1">Learning to Program in Perll </li><li style="flex:1">5</li></ul><p>Contents </p><p><strong>18 System Dependencies. . . . . . . . . . . . . . . . . . . . . . . . . . 197 </strong></p><p>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 </p><p><strong>19 More than Simple Lists and Hashes!. . . . . . . . . . . . . . 205 </strong></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 </em></p><p><strong>20 Handling Dates and Time . . . . . . . . . . . . . . . . . . . . . . . 213 </strong></p><p>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 </p><p><em>Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 </em></p><p><strong>21 Practical Example – Perl in use . . . . . . . . . . . . . . . . . . 221 </strong></p><p>21.1 The requirement . . . . . . . . . . . . . . . . . . . . . . . . . 221 21.2 Plain Old Documentation (POD). . . . . . . . . . . . . 231 21.3 Possible enhancements . . . . . . . . . . . . . . . . . . . 231 </p><p><strong>22 Libraries and Resources. . . . . . . . . . . . . . . . . . . . . . . . 233 </strong></p><p>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 </p><p><strong>23 Perl 6 Look Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 </strong></p><p>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 </p><p><strong>24 A Quick Look Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 </strong></p><p>24.1 Fundamental and advanced topics. . . . . . . . . . . 255 </p><p></p><ul style="display: flex;"><li style="flex:1">6</li><li style="flex:1">Well House Consultants </li></ul><p>Contents </p><p>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 </p><p><strong>Appendix. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 </strong></p><p>Exercise sample answers . . . . . . . . . . . . . . . . . 265 CPAN sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Common Errors in Perl . . . . . . . . . . . . . . . . . . . 273 </p><p><strong>WHC Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 </strong></p>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages286 Page
-
File Size-