Beginning Perl Programming Self-Study Guide & Student Manual 2018-19 Edition William "Bo" Rothwell A publication by Copyright One Course Source, 2018 ALL RIGHTS RESERVED This publication contains proprietary and confidential information, which is the property of One Course Source, Inc No part of this publication is be reproduced, copied, disclosed, transmitted, stored in a retrieval system or translated into any human or computer language, in any form, by any means, in whole or in part, without the prior express written consent of One Course Source THIS PUBLICATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT REFERENCES TO CORPORATIONS, THEIR SERVICES AND PRODUCTS, ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED IN NO EVENT SHALL ONE COURSE SOURCE BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING OUT OF OR IN CONNECTION WITH THE USE OF THIS INFORMATION Descriptions of, or references to, products or publications within this publication not imply endorsement of that product or publication One Course Source makes no warranty of any kind with respect to the subject matter included herein, the products listed herein, or the completeness or accuracy of this publication One Course Source specifically disclaims all warranties, express, implied or otherwise, including without limitation, all warranties of merchantability and fitness for a particular purpose THIS PUBLICATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW EDITIONS OF THE PUBLICATION ONE COURSE SOURCE MAY MAKE IMPROVEMENTS AND/OR CHANGES IN THE PRODUCT(S) AND/OR THE PROGRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME This notice may not be removed or altered Module Table of Contents Introduction Unit One Meet Perl Page 1.1 Origin of Perl 16 1.2 Perl development environments 17 1.3 Perl Versions 21 1.4 Invoking Perl 24 1.5 Perl Documentation 30 1.6 Perl Resources 36 1.7 Lab Exercises 37 Unit Two Scalar Variables Page 2.1 Numeric Literals 39 2.2 Manipulating Numbers 40 2.3 String Literals 43 2.4 Manipulating Strings 44 2.5 Strings vs Numbers 48 2.6 Single vs Double Quotes 52 2.7 Scalars Variables 56 2.8 Undefined variables 57 2.9 Auto increment and Auto decrement 61 2.10 Reading data from the user 66 2.11 chomp & chop 67 2.12 Curly braces 70 2.13 Additional Resources 71 2.14 Lab Exercises 72 Table of Contents ©2018 One Course Source, Inc Page Unit Three Array Variables Page 3.1 .Array Variables 74 3.2 Referencing Array Elements 75 3.3 Adding & removing elements in an array 79 3.4 The splice function 82 3.5 Using for loops 85 3.6 Using foreach loops 87 3.7 The reverse statement 91 3.8 The sort operator 92 3.9 The qw and qq statements 95 3.10 Arrays used in scalar context 96 3.11 Additional Resources 97 3.12 Lab Exercises 98 Unit Four Associate Array Variables Page 4.1 Associative Array Variables 100 4.2 Creating associative arrays 102 4.3 Accessing values with keys and foreach 105 4.4 Accessing values with "while-each" loops 107 4.5 The values statement 112 4.6 Reverse searching an associative array 114 4.7 Removing associative array keys and values 115 4.8 exists vs defined 116 4.9 Special Variables 119 4.10 The Environment Variables 120 4.11 The Argument Variable 122 4.12 Additional Resources 123 4.13 Lab Exercises 124 Table of Contents ©2018 One Course Source, Inc Page Unit Five Flow Control Page 5.1 Blocks 126 5.2 The if Statement 127 5.3 The unless Statement 133 5.4 The switch statement 134 5.5 The given statement 137 5.6 The while Statement 139 5.7 The until Statement 173 5.8 The Statement 141 5.9 Loop control: last 144 5.10 Loop control: next 148 5.11 Additional Resources 149 5.12 Lab Exercises 150 Unit Six Conditional Expressions Page 6.1 .Numeric Comparison 152 6.2 String Comparison 153 6.3 Difference between string and numeric comparison 155 6.4 Pattern Matching 158 6.5 Using the Outcome of a Statement 159 6.6 File Test Conditions 161 6.7 Complex Conditional Expressions 165 6.8 Understand and/or versus &&/|| 168 6.9 Using parentheses 170 6.10 Short circuiting 171 6.11 Additional Resources 173 6.12 Lab Exercises 174 Table of Contents ©2018 One Course Source, Inc Page Unit Seven Basic Input and Output Page 7.1 Reading Input 176 7.2 while & until Loops 177 7.3 Record Separator Variable 179 7.4 The Diamond Operator 181 7.5 The Default Variable 184 7.6 Using parentheses 186 7.7 Additional Resources 187 7.8 Lab Exercises 188 Unit Eight Advanced Input and Output Page 8.1 Filehandles 190 8.2 The die and warn Statements 191 8.3 Opening and Reading from Files 195 8.4 Opening and Writing to Files 198 8.5 Reading a Block of a Filehandle 200 8.6 Reading a Single Character 202 8.7 Piping in Perl 203 8.8 The format Statement 207 8.9 Here Documents 219 8.10 Additional Resources 222 8.11 Lab Exercises 223 Unit Nine Pattern Matching Page 9.1 Pattern Matching vs Wildcards 225 9.2 Matching, Substituting and Translation 226 9.3 Modifiers 229 9.4 Regular Expressions: Metacharacters 232 9.5 Regular Expressions: Classes 249 9.6 Regular Expressions: Backreferencing 256 9.7 Additional Resources 263 9.8 Lab Exercises 264 Table of Contents ©2018 One Course Source, Inc Page Unit Ten Perl Utilities Page 10.1 split 266 10.2 join 270 10.3 substr 272 10.4 index 275 10.5 rindex 276 10.6 grep 278 10.7 srand & rand 280 10.8 sleep 282 10.9 Additional Resources 284 10.10 Lab Exercises 285 Unit Eleven Filesystem & Process Control Page 11.1 Controlling the Filesystem within Perl 287 11.2 Working with Directories 289 11.3 Working with Files 294 11.4 Back-Quoting 298 11.5 The System Statement 299 11.6 Additional Resources 301 11.7 Lab Exercises 302 Unit Twelve Functions Page 12.1 Creating Functions 304 12.2 Invoking Functions 305 12.3 Returning Values from Functions 307 12.4 Passing Parameters 310 12.5 Scope of Variables 314 12.6 .local() vs my() 316 12.7 Additional Resources 324 12.8 Lab Exercises 325 Table of Contents ©2018 One Course Source, Inc Page Unit Thirteen Using Modules Page 13.1 What are Modules? 327 13.2 Loading Modules with use 328 13.3 Other functions of use 330 13.4 Additional Resources 335 13.5 Lab Exercises 336 Unit Fourteen Debugging Perl Page 14.1 The -w switch 338 14.2 The Perl Debugger 341 14.3 Debugger Commands 342 14.4 Additional Resources 343 14.5 Lab Exercises 344 Table of Contents ©2018 One Course Source, Inc Page Introduction About this course This manual was designed with the goal of assisting instructors in their efforts of teaching students to be able to create Perl programs Typographical syntax Examples in this text of commands will appear in bold text and the output of the commands will appear in italic text The commands and the output of the commands will be placed in a box to separate them from other text Example: [student@ocs student]$ pwd /home/student Note: "[student@ocs student]$" is a prompt, a method the shell uses to say “I’m ready for a new command” Bold text within a sentence will indicate an important term or a command Files and directories are highlighted by being placed in courier font Introduction ©2018 One Course Source, Inc Page Using this manual while in class In many ways, class manuals are different from textbooks Textbooks are often filled with lengthy paragraphs that explain a topic in detail Unfortunately, this style doesn’t work well in a classroom environment Class manuals often are much more concise than textbooks It's difficult to follow the instructor’s example and read lengthy paragraphs in a book at the same time For this purpose, class manuals are often more terse Lab Exercises The lab exercises provided in this class are intended to provide practical, hands on experience with programming Perl Students are strongly encouraged to perform the labs provided at the end of each Unit to reinforce the knowledge provided in class Introduction ©2018 One Course Source, Inc Page 10 ... through: Document perlintro perlrequick perlretut perlstyle perlcheat perltrap perldebtut perlsyn Description Perl introduction for beginners Perl regular expressions quick start Perl regular expressions... script 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [student@ ocs student] $ chmod a+x 1_second.pl [student@ ocs student] $ /1_second.pl This is my second Perl program [student@ ocs student] $ The first... the Perl cheat sheet, execute the perldoc perlcheat command – Perl Fundamentals ? ?2018 One Course Source, Inc Page 33 Try it! Execute the following command to display the ? ?perl style guide? ??: perldoc