Introduction to Perl
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Perl #!/usr/bin/perl −w # find−a−func use strict; $_=’$;="per l";map{map {s}^\s+}} ;$_{$_}++unless(/[^a− z]/)}split(/ [\s,]+/)i f(/alpha. *$;/i../w ait/)}‘$; doc\040$; toc‘;;;@[=k eys%_;$; =20;$:=15;;for(0..($;*$:−1 )){$;[$_]="_" ;}until($%++>3*$;||@]>2*$:−3){@_=split(//,splice(@[,rand( @[),1));if(3>@_){next;}$~=int(rand($;));$^=int(rand($:)); $−=$~+$^*$;;my$Erudil=0;{if($Erudil++>2*$:){next;}$a=(−1, 0,1)[rand(3)];$b=(−1,0,1)[rand(3)];unless(($a||$b)&&$~ +$a*@_<=$;&&$~+$a*@_>=0&&$^+$b*@_<=$:&&$^+$b*@_>=0){re do;;}my$llama=0;;for(0..$#_){unless($;[$−+$a*$_+$b* $;*$_]eq$_[$_]||$;[$−+$a*$_+$b*$;*$_]eq"_"){$llam a++;last;}}if($llama){redo;}push@],join("",@_);f or(0..$#_){$;[$−+$a*$_+$b*$;*$_]=$_[$_];}}}@_ =sort@];unshift@_ ,"Find:","−"x5;for$a(0. .$:−1){for$b(0. .$;−1){$~=("a".."z") [rand(26)];$_ ="$;[$a*$;+$b]". $";s;_;$~; ;print;}$_=s hift@_|| $";;print$ ",$", $_,$ /;$_ =shi ft@_ ||$ ";pr int $"x $;, $"x $;, $", $", $_ ,$/;; ;}’ ;;; s[\s+] $$g; eval; Kirrily Robert Paul Fenwick Jacinta Richardson Introduction to Perl by Kirrily Robert, Paul Fenwick, and Jacinta Richardson Copyright © 1999-2000 Netizen Pty Ltd Copyright © 2000 Kirrily Robert Copyright © 2001 Obsidian Consulting Group Pty Ltd Copyright © 2001-2005 Paul Fenwick ([email protected]) Copyright © 2001-2005 Jacinta Richardson ([email protected]) Copyright © 2001-2005 Perl Training Australia Open Publications License 1.0 Cover artwork Copyright (c) 2000 by Stephen B. Jenkins. Used with permission. The use of a llama 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 latest version 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 from the copyright holder. This document is a revised and edited copy of the training notes originally created by Kirrily Robert 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/spork This training manual is maintained by Perl Training Australia, and can be found at http://www.perltraining.com.au/notes.html. This is revision 4.20 of the Perl Training Australia’s "Introduction to Perl" training manual. Table of Contents 1. About Perl Training Australia....................................................................................................... 1 Training....................................................................................................................................... 1 Consulting ................................................................................................................................... 1 Contact us.................................................................................................................................... 1 2. Introduction..................................................................................................................................... 3 Credits ......................................................................................................................................... 3 Course outline ............................................................................................................................. 3 Day 1 ................................................................................................................................. 3 Day 2 ................................................................................................................................. 3 Assumed knowledge ................................................................................................................... 3 Module objectives ....................................................................................................................... 3 Platform and version details........................................................................................................4 The course notes.......................................................................................................................... 4 Other materials............................................................................................................................ 5 3. What is Perl..................................................................................................................................... 7 In this chapter.............................................................................................................................. 7 Perl’s name and history............................................................................................................... 7 Typical uses of Perl ..................................................................................................................... 7 Text processing..................................................................................................................7 System administration tasks ..............................................................................................7 CGI and web programming...............................................................................................7 Database interaction ..........................................................................................................8 Other Internet programming..............................................................................................8 Less typical uses of Perl ....................................................................................................8 What is Perl like? ........................................................................................................................ 8 The Perl Philosophy.................................................................................................................... 9 There’s more than one way to do it ...................................................................................9 A correct Perl program......................................................................................................9 Three virtues of a programmer..........................................................................................9 Laziness....................................................................................................................9 Impatience................................................................................................................9 Hubris.......................................................................................................................9 Three more virtues...........................................................................................................10 Share and enjoy!..............................................................................................................10 Parts of Perl...............................................................................................................................10 The Perl interpreter..........................................................................................................10 Manuals/Documentation..................................................................................................10 Perl Modules....................................................................................................................11 Chapter summary ......................................................................................................................11 4. A brief guide to perldoc................................................................................................................13 Using perldoc............................................................................................................................13 Exercise ...........................................................................................................................13 Language features and tutorials ................................................................................................13 Looking up functions ................................................................................................................13 Looking up modules..................................................................................................................14 Perl Training Australia (http://www.perltraining.com.au/) iii 5. Creating and running a Perl program........................................................................................15 In this chapter............................................................................................................................15 Logging into your account........................................................................................................15 Our first Perl program ...............................................................................................................15 Running a Perl program from the command line......................................................................15 Executing code..........................................................................................................................16 The "shebang" line for Unix............................................................................................16 The "shebang" line for