Learning to Program in Perl
Total Page:16
File Type:pdf, Size:1020Kb
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 First Edition February 2000 1.1 Minor additions June 2000 1.2 Compliation of modules October 2000 1.3 Name change, revisions April 2002 1.4 Added modules September 2002 1.5 Added modules January 2003 1.6 Updated modules February 2003 1.7 Updated modules 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 designa- tions 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 1Introduction . 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 2Hello Perl World. 17 2.1 Let's study a first program. 17 2.2 Summary . 21 Exercise . 22 3Variables 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 4Perl 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 8Initial 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 . ..