Programming Perl - Perl Training

Transcription

Programming Perl#!/usr/bin/perl wuse strict; ’eval("seek\040DATA,0,0;");foreach(1.2){ DATA ;}my@camel1hump;my camel;my Camel ;while( DATA ){ sprintf("% 69s", );my@dromedary1 split(//);if(defined( DATA )){@camel1hump split(//);}while(@dromedary1){my camel1hump 0;my CAMEL 3;if(defined( shift(@dromedary1))&&/\S/){ camel1hump 1 CAMEL;} CAMEL ;if(defined( shift(@dromedary1))&&/\S/){ camel1hump 1 CAMEL;} CAMEL ;if(defined( shift(@camel1hump))&&/\S/){ camel1hump 1 CAMEL;} CAMEL ;if(defined( shift(@camel1hump))&&/\S/){ camel1hump 1 CAMEL;;} camel. (split(//,"\040.m‘{/J\047\134}L 7FX"))[ camel1hump];} camel. "\n";}@camel1hump split(/\n/, camel);foreach(@camel1hump){chomp; Camel 5678/JL7F\175\173\047‘/; reverse;print" \040 Camel\n";}foreach(@camel1hump){chomp; Camel 175\173\047‘/; reverse;print"\040 Camel\n";}#japh );undef /; DATA ;s \s* g;();;s; .* ;;;map{eval"print\" \"";}/.{4}/g; DATA\124\150\145\040\165\163\145\040\157\1 46\040\1 41\040\143\141 \155\145\1 54\040\151\155\ 141\147\145\0 40\151\156 \040\141\163\16 3\157\143\151\141\16 4\151\157\156\040\167 \146\040\117\047\122\145\151\154\1 44\040\167\151\164\1 Kirrily RobertPaul FenwickJacinta Richardson

Programming Perlby Kirrily Robert, Paul Fenwick, and Jacinta RichardsonCopyright 1999-2000 Netizen Pty LtdCopyright 2000 Kirrily RobertCopyright 2001 Obsidian Consulting Group Pty LtdCopyright 2001-2011 Paul Fenwick (pjf@perltraining.com.au)Copyright 2001-2011 Jacinta Richardson (jarich@perltraining.com.au)Copyright 2001-2011 Perl Training AustraliaOpen Publications License 1.0Cover artwork Copyright (c) 2000 by Stephen B. Jenkins. Used with permission.The use of a camel image with the topic of Perl is a trademark of O’Reilly & Associates, Inc. Used with permission.This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latestversion is presently available at http://www.opencontent.org/openpub/).Distribution of this work or derivative of this work in any standard (paper) book form is prohibited unless prior permission is obtained fromthe copyright holder.This document is a revised and edited copy of the Introduction to Perl and Intermediate Perl training notes originally created by KirrilyRobert and Netizen Pty Ltd. These revisions were made by Paul Fenwick and Jacinta Richardson.Copies of the original training manuals can be found at http://sourceforge.net/projects/sporkThis training manual is maintained by Perl Training Australia, and can be found at http://www.perltraining.com.au/notes.htmlThis is version 1.39 of Perl Training Australia’s "Programming Perl" training manual.

Table of Contents1. About Perl Training Australia . 1Training . 1Consulting . 1Contact us. 12. Introduction. 3Credits . 3Course outline . 3Day 1 . 3Day 2 . 3Day 3 . 3Day 4 . 3Day 5 . 4Assumed knowledge . 4Platform and version details. 4The course notes. 4Other materials . 53. What is Perl . 7In this chapter. . 7License . 7Perl’s name and history . 7Typical uses of Perl . 7Text processing . 7System administration tasks . 7CGI and web programming. 8Database interaction . 8Other Internet programming. 8Less typical uses of Perl . 8What is Perl like? . 8The Perl Philosophy . 9There’s more than one way to do it . 9A correct Perl program. . 9Three virtues of a programmer. 9Laziness. 9Impatience. 9Hubris. 10Three more virtues. 10Share and enjoy! . 10Parts of Perl . 10The Perl interpreter. 10Manuals/Documentation. 11Perl Modules. 11Chapter summary . 114. A brief guide to perldoc. 13Using perldoc . 13Other ways to access perldoc . 13Exercise . 13Language features and tutorials . 13Looking up functions . 14Perl Training Australia (http://perltraining.com.au/)iii

Searching the FAQs. 14Looking up modules. 145. Creating and running a Perl program. 17In this chapter. . 17Logging into your account . 17Our first Perl program . 17Running a Perl program from the command line. 17Executing code . 18The "shebang" line for Unix. 18The "shebang" line for non-Unixes . 19Command line options and warnings. 19Lexical warnings. 19Comments . 20Block comments . 20END . 20Chapter summary . 206. Perl variables. 23In this chapter. . 23What is a variable? . 23Variable names . 23Strictures . 23Using the strict pragma (predeclaring variables). 24Exercise. 24Starting your Perl program. 25Specifying your version. 25Scalars . 25Double and single quotes . 27Exercise . 27Special characters . 27Advanced variable interpolation. 28say (Perl 5.10.0 ) . 28Exercises. 29Arrays. 29Initialising an array. 29Reading and changing array values . 29Array slices . 30Array interpolation . 30Counting backwards . 31Finding out the size of an array . 31Using qw// to populate arrays. 31Printing out the values in an array . 32A quick look at context. 32What’s the difference between a list and an array? . 33Exercises. 33Advanced exercises . 34Hashes . 34Initialising a hash. 34Reading hash values . 35Adding new hash elements . 35Changing hash values . 35Deleting hash values. 36ivPerl Training Australia (http://perltraining.com.au/)

Finding out the size of a hash . 36Other things about hashes. 36Exercises. 37Special variables. 37The special variable . 37@ARGV - a special array. 38%ENV - a special hash . 39Exercises. 39Programming practices . 39Perltidy . 39Perl Critic. 40Chapter summary . 407. Operators and functions. 43In this chapter. . 43What are operators and functions?. 43Operators. 43Arithmetic operators. 43String operators . 44Exercises. 45Advanced exercise . 45Other operators . 45Functions. 45Types of arguments. 46Return values . 47More about context . 47Some easy functions. 48String manipulation . 48Finding the length of a string . 48Case conversion . 48chop() and chomp(). 49Reversing a string. 49String substitutions with substr() . 50Exercises . 50Numeric functions . 50Type conversions . 51Manipulating lists and arrays. 51push, pop, shift and unshift. 51Ordering lists. 52Converting strings to lists, and vice versa (split and join) . 52Exercises. 53Hash processing. 53Reading and writing files. 54Time. 54Exercises. 54Chapter summary . 548. Conditional constructs. 57In this chapter. . 57What is a conditional statement? . 57What is truth? . 57The if conditional construct . 57So what is a BLOCK? . 58Perl Training Australia (http://perltraining.com.au/)v

Scope . 59Comparison operators . 60Exercises . 61Existence and definitiveness. 61Exercise. 62Boolean logic operators . 63Logic operators and short circuiting . 64Boolean assignment . 65Loop conditional constructs . 65while loops. 66for and foreach. 66Exercises. 68Practical uses of while loops: taking input from STDIN . 68Exercises. 69Named blocks. 69Breaking out or restarting loops. 69Practical exercise. 71given and when. 71Chapter summary . 729. Subroutines. 73In this chapter. . 73Introducing subroutines. 73What is a subroutine? . 73Why use subroutines?. 73Using subroutines in Perl. 73Calling a subroutine . 74Passing arguments to a subroutine . 75Passing in scalars . 75Passing in arrays and hashes. 76Returning values from a subroutine . 77Exercises . 78Chapter summary . 7810. Regular expressions . 81In this chapter. . 81What are regular expressions?. 81Regular expression operators and functions. 81m/PATTERN/ - the match operator . 81s/PATTERN/REPLACEMENT/ - the substitution operator. 82Exercises . 82Binding operators . 82Easy modifiers . 83Meta characters . 83Some easy meta characters. 83Quantifiers . 85Exercises. 86Grouping techniques . 86Character classes . 86Exercises . 87Alternation. 87The concept of atoms. 88Exercises . 89viPerl Training Australia (http://perltraining.com.au/)

Chapter summary .

This document is a revised and edited copy of the Introduction to Perl and Intermediate Perl training notes originally created by Kirrily Robert and Netizen Pty Ltd. These re