This work (Netizen "Introduction to Perl" training module notes) is licensed under the Open Publication License.
LICENSE
Terms and Conditions for Copying, Distributing, and Modifying
Items other than copying, distributing, and modifying the Content with which this license wasdistributed (such as using, etc.) are outside the scope of this license.
1. You may copy and distribute exact replicas of the OpenContent (OC) as you receive it, in anymedium, provided that you conspicuously and appropriately publish on each copy an appropriatecopyright notice and disclaimer of warranty; keep intact all the notices that refer to this License andto the absence of any warranty; and give any other recipients of the OC a copy of this License alongwith the OC. You may at your option charge a fee for the media and/or handling involved in creatinga unique copy of the OC for use offline, you may at your option offer instructional support for the OCin exchange for a fee, or you may at your option offer warranty in exchange for a fee. You may notcharge a fee for the OC itself. You may not charge a fee for the sole service of providing access toand/or use of the OC via a network (e.g. the Internet), whether it be via the world wide web, FTP, orany other method.
2. You may modify your copy or copies of the OpenContent or any portion of it, thus forming worksbased on the Content, and distribute such modifications or work under the terms of Section 1above, provided that you also meet all of these conditions:
a) You must cause the modified content to carry prominent notices stating that you changed it, theexact nature and content of the changes, and the date of any change.
b) You must cause any work that you distribute or publish, that in whole or in part contains or isderived from the OC or any part thereof, to be licensed as a whole at no charge to all third partiesunder the terms of this License, unless otherwise permitted under applicable Fair Use law.
These requirements apply to the modified work as a whole. If identifiable sections of that work arenot derived from the OC, and can be reasonably considered independent and separate works inthemselves, then this License, and its terms, do not apply to those sections when you distributethem as separate works. But when you distribute the same sections as part of a whole which is awork based on the OC, the distribution of the whole must be on the terms of this License, whosepermissions for other licensees extend to the entire whole, and thus to each and every partregardless of who wrote it. Exceptions are made to this requirement to release modified worksfree of charge under this license only in compliance with Fair Use law where applicable.
3. You are not required to accept this License, since you have not signed it. However, nothing elsegrants you permission to copy, distribute or modify the OC. These actions are prohibited by law ifyou do not accept this License. Therefore, by distributing or translating the OC, or by deriving worksherefrom, you indicate your acceptance of this License to do so, and all its terms and conditionsfor copying, distributing or translating the OC.
NO WARRANTY
4. BECAUSE THE OPENCONTENT (OC) IS LICENSED FREE OF CHARGE, THERE IS NOWARRANTY FOR THE OC, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHENOTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIESPROVIDE THE OC "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED ORIMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OFMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USEOF THE OC IS WITH YOU. SHOULD THE OC PROVE FAULTY, INACCURATE, OR OTHERWISEUNACCEPTABLE YOU ASSUME THE COST OF ALL NECESSARY REPAIR OR CORRECTION.
5. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILLANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MIRROR AND/OR REDISTRIBUTETHE OC AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANYGENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USEOR INABILITY TO USE THE OC, EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEENADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
Additionally:
6. If you offer training based upon this OpenContent, you must prominently display a notice stating whether or notyou are a Netizen Certified Training Organisation on the Open Contentitself and on any material advertising or publicising your training.
a) If you are a Netizen Certified Training Organisation, you must statethat you are a Netizen Certified Training Organisation and display theNetizen Certified Training Organisation logo. You must also provide aURL for more information, namely http://netizen.com.au/services/training/ncto/
b) If you are not a Netizen Certified Training Organisation, you muststate that you are not a Netizen Certified Training Organisation. Youmay not use the Netizen Certified Training Organisation logo. Youmust also provide a URL for more information, namely http://netizen.com.au/services/training/ncto/
Netizen and the N logo are trademarks of Netizen Pty Ltd. All rightsreserved.
- Table of Contents
- 1. Introduction
- Course outline
- Assumed knowledge
- Module objectives
- Platform and version details
- The course notes
- Other materials
- 2. What is Perl
- In this chapter...
- Perl's name
- Typical uses of Perl
- Text processing
- System administration tasks
- CGI and web programming
- Database interaction
- Other Internet programming
- Less typical uses of Perl
- What is Perl like?
- The Perl Philosophy
- There's more than one way to do it
- A correct Perl program...
- Three virtues of a programmer
- Three more virtues
- Share and enjoy!
- Parts of Perl
- The Perl interpreter
- Manuals
- Perl Modules
- Chapter summary
- 3. Creating and running a Perl program
- In this chapter...
- Logging into your account
- Using perldoc
- Using the editor
- Our first Perl program
- Running a Perl program from the command line
- The "shebang" line
- Comments
- Command line options
- Chapter summary
- 4. Perl variables
- In this chapter...
- What is a variable?
- Variable names
- Variable scoping and the strict pragma
- Arguments in favour of strictness
- Arguments against strictness
- Using the strict pragma
- Scalars
- Double and single quotes
- Exercises
- Arrays
- A quick look at context
- What's the difference between a list and an array?
- Exercises
- Advanced exercises
- Hashes
- Initialising a hash
- Reading hash values
- Adding new hash elements
- Other things about hashes
- What's the difference between a hash and an associative array?
- Exercises
- Special variables
- The first special variable, $_
- @ARGV - a special array
- %ENV - a special hash
- Chapter summary
- 5. Operators and functions
- In this chapter...
- What are operators and functions?
- Operators
- Arithmetic operators
- String operators
- Exercises
- File operators
- Other operators
- Functions
- Types of arguments
- Return values
- More about context
- Some easy functions
- String manipulation
- Numeric functions
- Type conversions
- Manipulating lists and arrays
- Hash processing
- Reading and writing files
- Time
- Exercises
- Chapter summary
- 6. Conditional constructs
- In this chapter...
- What is a block?
- Scope
- What is a conditional statement?
- What is truth?
- Comparison operators
- Existence and Defined-ness
- Boolean logic operators
- Using boolean logic operators as short circuit operators
- Types of conditional constructs
- if statements
- while loops
- for and foreach
- Exercises
- Practical uses of while loops: taking input from STDIN
- Named blocks
- Breaking out of loops
- Chapter summary
- 7. Subroutines
- In this chapter...
- Introducing subroutines
- Calling a subroutine
- Passing arguments to a subroutine
- Returning values from a subroutine
- Exercises
- Chapter summary
- 8. Regular expressions
- In this chapter...
- What are regular expressions?
- Regular expression operators and functions
- m/PATTERN/ - the match operator
- s/PATTERN/REPLACEMENT/ - the substitution operator
- Binding operators
- Metacharacters
- Some easy metacharacters
- Quantifiers
- Greediness
- Exercises
- Grouping techniques
- Character classes
- Alternation
- The concept of atoms
- Exercises
- Chapter summary
- 9. Practical exercises
- 10. Conclusion
- What you've learnt
- Where to now?
- Further reading
- A. Unix cheat sheet
- B. Editor cheat sheet
- vi
- Running
- Using
- Exiting
- Gotchas
- Help
- pico
- Running
- Using
- Exiting
- Gotchas
- Help
- joe
- Running
- Using
- Exiting
- Gotchas
- Help
- jed
- Running
- Using
- Exiting
- Gotchas
- Help
- C. ASCII Pronunciation Guide
Chapter 1. Introduction
Welcome to Netizen's Introduction to Perl training module.This is a one-day training module in which you will learn how to program in thePerl programming language.
Course outlineWhat is Perl? (30 minutes)
Creating and running a Perl program (45 minutes)
Morning tea (15 minutes)
Variable types (45 minutes)
Operators and Functions (60 minutes)
Lunch break (60 minutes)
Conditional constructs (45 minutes)
Subroutines (30 minutes)
Afternoon tea (15 minutes)
Regular expressions (45 minutes)
Practical exercises (until finish)
Assumed knowledgeTo gain the most from this course, you should:
Module objectivesUnderstand the history and philosophy behind the Perl programming language
Know where to find additional information aboutPerl
Write simple Perl scripts and run them from the Unix command line
Use Perl's command line options to enablewarnings
Understand Perl's three main data types and how to use them
Use Perl's strict pragma to enforce lexical scoping and better coding
Understand Perl's most common operators and functions and how to use them
Understand and use Perl's conditional and looping constructs
Understand and use subroutines in Perl
Understand and use simple regular expressions for matching and substitution
Platform and version detailsThis module is taught using Unix or a Unix-like operating system. Mostof what is learnt will work equally well on Windows NT or otheroperating systems; your instructor will inform you throughout the courseof any areas which differ.
All Netizen's Perl training courses use Perl 5, the most recent majorrelease of the Perl language. Perl 5 differs signficantly from previousversions of Perl, so you will need a Perl 5 interpreter to use what youhave learnt. However, older Perl programs should work fine under Perl5.
The course notesThese course notes contain material which will guide you through thetopics listed above, as well as appendices containing other usefulinformation.
The following typographical conventions are used in these notes:
System commands appear in this typeface
Literal text which you should type in to the command line or editorappears as monospaced font.
Keystrokes which you should type appear like this:ENTER. Combinations of keys appear like this:CTRL-D
Program listings and other literal listings of what appears on thescreen appear in a monospaced font like this.Parts of commands or other literal text which should be replaced by your own specific values appears like this
Note: Notes and tips appear offset from the text like this.
Advanced: Notes which are marked "Advanced" are for those who are racing ahead orwho already have some knowledge of the topic at hand. The informationcontained in these notes is not essential to your understanding of thetopic, but may be of interest to those who want to extend theirknowledge.
Readme: Notes marked with "Readme" are pointers to more information which can befound in your textbook or in online documentation such as manual pagesor websites.
Other materialsIn addition to these notes, you should have a copy of the requiredtext book for this course: Programming Perl (2nd edition) byLarry Wall, Tom Christiansen and Randal L. Schwartz, more commonly referred to as "the Camel book". The Camel book will beused throughout the day, and will be a valuable reference to take homeand keep next to your computer.
You will also have received a floppy disk containing these notes inHTML form (with working links to external resources etc) and all theexercises and data used in this course.
Chapter 2. What is Perl
In this chapter...This section describes Perl and its uses. You will learn about thishistory of Perl, the main areas in which it is commonly used, and alittle about the Perl community and philosophy. Lastly, you will findout how to get Perl and what software comes as part of the Perldistribution.
Perl's namePerl has been said to stand for "Practical Extraction and ReportingLanguage" (by it's fans) or "Pathologically Eclectic Rubbish Lister"(by its detractors). In fact, Perl is not an acronym; it's a shortenedversion of the program's original name, "pearl", and when you're talkingabout the language it's spelt with acapital "P" and lowercase "erl", not all capitals as is sometimesseen (especially in job advertisements posted by contract agencies).When you're talking about the Perl interpreter, it's spelt in all lowercase: perl.
Perl has been described as everything from "line noise" to "theSwiss-army chainsaw of programming languages". The latter of thesenicknames gives some idea of how programmers see Perl - as a verypowerful tool that does just about everything.
Typical uses of PerlText processingPerl's original main use was text processing. It is exceedinglypowerful in this regard, and can be used to manipulate textual data,reports, email, news articles, log files, or just about any kind oftext, with great ease.
System administration tasksSystem administration is made easy with Perl. It's particularlyuseful for tying together lots of smaller scripts, working with filesystems, networking, and so on.
CGI and web programmingSince HTML is just text with built-in formatting, Perl can be used toprocess and generate HTML. Perl is probably the most popular languagearound for web development, and there are many tools and scriptsavailable for free.
Database interactionPerl's DBI module makes interacting with all kinds of databases --- fromOracle down to comma-separated variable files --- easy and portable.Perl is increasingly being used to write large database applications,especially those which provide a database backend to a website.
Other Internet programmingPerl modules are available for just about every kind of Internetprogramming, from Mail and News clients, interfaces to IRC and ICQ,right down to lower level Socket programming.
Less typical uses of PerlPerl is used in some unusual places as well. The Human Genome Projectrelies on Perl for DNA sequencing, NASA use Perl for satellite control,PDL (Perl Data Language, pron. "piddle") makes number-crunching easy,and there is even a Perl Object Environment (POE) which is used for event-driven state machines.
What is Perl like?The following (somewhat paraphrased) article, entitled "What isPerl", comes from The Perl Journal(Used with permission.)
Perl is a general purpose programming languagedeveloped in 1987 by Larry Wall. It has become the language of choicefor WWW development, text processing, Internet services, mail filtering,graphical programming, and every other task requiring portableand easily-developed solutions.
Perl is interpreted.This means that as soon as you write your program, you can run it -there's no mandatory compilation phase. The same Perl program can runon Unix, Windows, NT, MacOS, DOS, OS/2, VMS and the Amiga.
Perl is collaborative.The CPAN software archive contains free utilities written by the Perlcommunity, so you save time.
Perl is free.Unlike most other languages, Perl is not proprietary. The source codeand compiler are free, and will always be free.
Perl is fast.The Perl interpreter is written in C, and a decade of optimisations haveresulted in a fast executable.
Perl is complete.The best support for regular expressions in any language, internalsupport for hash tables, a built-in debugger, facilities for reportgeneration, networking functions, utilities for CGI scripts, databaseinterfaces, arbitrary-precision arithmetic - are all bundled with Perl.
Perl is secure.Perl can perform "taint checking" to prevent security breaches. You canalso run a program in a "safe" compartment to avoid the risks inherentin executing unknown code.
Perl is open for business.Thousands of corporations rely on Perl for their information processingneeds.
Perl is simple to learn.Perl makes easy things easy and hard things possible. Perl handlestedious tasks for you, such as memory allocation and garbage collection.
Perl is concise.Many programs that would take hundreds or thousands of lines in otherprogramming languages can be expressed in a pageful of Perl.
Perl is object oriented.Inheritance, polymorphism, and encapsulation are all provided by Perl'sobject oriented capabilities.
Perl is flexibleThe Perl motto is "there's more than one way to do it." The languagedoesn't force a particular style of programming on you. Write whatcomes naturally.
Perl is fun.Programming is meant to be fun, not only in the satisfaction of seeingour well-tuned programs do our bidding, but in the literary act ofcreative writing that yields those programs. With Perl, the journey isas enjoyable as the destination.
The Perl PhilosophyThere's more than one way to do itThe Perl motto is "there's more than one way to do it" - oftenabbreviated TMTOWTDI. What this means is that for any problem, therewill be multiple ways to approach it using Perl. Some will be quicker,more elegant, or more readable than others, but that doesn't make themwrong.
A correct Perl program..."... is one that does the job before your boss fires you." That's inthe preface to the Camel book, which is highly recommended reading.
Of course, some Perl programs are more correct than others, but whileelegance is a fine thing to strive for, most Perl people realise thatsoemtimes you just have to write a quick and dirty hack that'll keepthings running for the mean time. If you get the time to make itbeautiful later, so much the better.
Three virtues of a programmerThe Camel book contains the following entries in its glossary:
LazinessThe quality that makes you go to great effort to reduce overall energyexpenditure. It makes you write labor-saving programs that other peoplewill find useful, and document what you wrote so you don't have toanswer so many questions about it. Hence, the first great virtue of aprogrammer.
ImpatienceThe anger you feel when the computer is being lazy. This makes youwrite programs that don't just react to your needs, but actuallyanticipate them. Or at least pretend to. Hence, the second greatvirtue of a programmer.
HubrisExcessive pride, the sort of thing Zeus zaps you for. Also the qualitythat makes you write (and maintain) programs that other people won'twant to say bad things about. Hence, the third great virtue of aprogrammer.
Three more virtuesIn his "State of the Onion" keynote speech at The Perl Conference 2.0 in1998, Larry Wall described another three virtues, which are the virtuesof a community of programmers. These are:
Diligence
Patience
Humility
You may notice that these are the opposites of the first three virtues.However, they are equally necessary for Perl programmers who wish towork together, whether on a software project for their company or on anOpen Source project with many contributors around the world.
Share and enjoy!Perl is Open Source software, and most of the modules and extensions forPerl are also released under Open Source licenses of various kinds (Perlitself is released under dual licenses, the GNU General Public Licenseand the Artistic License, copies of which are distributed with thesoftware).
The culture of Perl is fairly open and sharing, and thousands ofvolunteers worldwide have contributed to the current wealth of softwareand knowledge available to us. If you have time, you should try andgive back some of what you've received from the Perl community.Contribute a module to CPAN, help a new Perl programmer to debug herprograms, or write about Perl and how it's helped you. Even buyingbooks written by the Perl gurus (like many of the O'Reilly Perl books)helps give them the financial means to keep supporting Perl.
Parts of PerlThe Perl interpreterThe main part of Perl is the interpreter. The interpreter is available for Unix, Windows, and many other platforms. The current version of Perlis 5.005, which is available from the Perl website or anyof a number of mirror sites (the Windows version is available from Activestate. The next release of Perl will beversion 5.6; the jump in version numbers is because it was felt that the number of additional features between releases warranted a largerdifference between version numbers.
ManualsAlong with the interpreter come the manuals for Perl. These areaccessed via the perldoc command or, on Unix systems, also via the man command. More than 30 manual pages come with the current version of perl. These can be found by typing man perl (or perldoc perl on non-Unix systems). The Perl FAQs (Frequently Asked Questions files) are available in perldoc format, and can be accessed by typing perldoc perlfaq
Watch while this is demonstrated; you'll get a chance to try it soon.
Perl ModulesPerl also comes with a collection of modules. These are Perl programswhich carry out certain common tasks, and can be included as commonlibraries in any Perl script. Less commonly used modules aren'tincluded with the distribution, but can be downloaded from (CPAN) andinstalled separately.
Chapter summaryCommon uses of Perl include
Perl is a general purpose programming language, distributed forfree via the Perl websiteand mirror sites
Perl includes excellent support for regular expressions, objectoriented programming, and other features
Perl allows a great degree of programmer flexibility - "There'smore than one way to do it".
The three virtues of a programmer are Laziness, Impatience andHubris. Perl will help you foster these virtues
The three virtues of a programmer in a group environmentare Diligence, Patience, and Humility.
Perl is a collaborative language - everyone is free to contribute to the Perl software and the Perl community
Parts of Perl include:
Chapter 3. Creating and running a Perl program
In this chapter...In this chapter we will be creating a very simple "Hello, world"program in Perl and exploring some of the basic syntax of the Perlprogramming language.
Logging into your accountYour username and password will have been given to you with thesecourse notes.
Table 3-1. Details required to connect to the Netizen training server
Hostname or IP address
Your username
Your password
Open the telnet program
Connect to the training server at the hostname or IPnumber given above
Login using the username and password you weregiven
You will find yourself at a Unix shell prompt. Hopefully (if you metthe pre-requisites of this course) you will now be able to see thatyour account has a subdirectory called exercises/ which are theexample scripts and exercises given in these course notes. If you'renot quite up to speed with Unix, there's a cheat-sheet in Appendix Aof these notes.
Using perldocOn the command line, type perldoc perl. You will findyourself in the Perl documentation pages. Here's how to get aroundinside the documentation:
Table 3-2. Getting around in perldoc
ActionKeystroke
Page downSPACE
Page upb
Quitq
Using the editorA Perl script is just a normal text file, which means that you can edit it using a normal text editor.
The system you are using has several editors available for your use,including vi, pico, and others. Those who are notalready familiar with vi should probably usepico,as it has a simpler interface. If you're an emacs user,sorry, our server doesn't have the resources to run it, but we do haveother editors which have an emacs emulation mode.
To edit a file using pico, type:
% pico filename(Note that the percent sign is your unix command line prompt - youdon't have to type it.)
To edit a file using vi, type:
% vi filenameFor other editors, just type the name of the editor followed by thename of the file you wish to edit.
A summary of editor commands appears in Appendix B in the back ofthese coursenotes, just in case you need them.
Incidentally, Appendix C contains a guide to pronouncing ASCIIcharacters, especially punctuation. This will help you translate perlinto spoken language, for ease of communication with otherprogrammers.
Our first Perl programWe're about to create our first, simple Perl script: a "hello world"program. There are a couple of things you should know in advance:
Perl programs (or scripts --- the words are interchangeable)consist of a series of statements
When you run the program, each statement is executed in turn, from the top of your script to the bottom. (There are two special caseswhere this doesn't occur, one of which --- subroutine declarations ---we'll be looking at later today)
Each statement ends in a semi-colon
Statements can flow over several lines
Whitespace (spaces, tabs and newlines) are ignored most places in a Perl script.
Now, just for practice, open a file called hello.pl in yourtext editor. Type in the following one-line Perl program:
print "Hello, world!\n";This one-line program calls the print function with a singleparameter, the string literal "Hello, world!" followed by anewline character.
Save it and exit.
Running a Perl program from the command lineWe can run the program from the command line by typing in:
perl hello.plYou should see this output:
Hello, world!This program should, of course, be entirely self-explanatory. Theonly thing you really need to note is the \n("backslash N") which denotes a new line.
The "shebang" lineSo what if we want to run our program from the command line withouthaving to type in the name of the Perl interpreter first?
You can make a file executable by typing:
% chmod +x hello.plat the command line. (For more information about the chmod command,type man chmod).
In order to let the shell know what to do with our program when we tryto run it with ./hello.pl from the command line, we put the following line at the top of our program:
#!/usr/bin/perlThat's what we call a "shebang" line (because the # is a "hash"sign, and the ! is referred to as a "bang", hence "hashbang" or"shebang"). It tells the system what to use to interpret ourscript. Of course, if the Perl interpreter were somewhere else on oursystem, we'd have to change the shebang line to reflect that.
CommentsIncidentally, comments in Perl start with a hash sign (#), either on aline on their own or after a statement. Anything after a hash is acomment.
# This is a hello world programprint "Hello, world!\n"; # print the message
Command line optionsPerl has a number of command line options, which you can specify onthe command line by typing perl options hello.pl or whichyou can include in the shebang line. Let's say you want to use the-w command line option to turn on warnings:
#!/usr/bin/perl -w(Incidentally, it's always a good idea to turn on warnings whileyou're developing something.)
Advanced: Setting the special variable $^W to a true value will locallydisable warnings (i.e. in the current block).
Readme: A full explanation of command line options can be found in the Camelbook on pages 330 to 337, or by typing perldoc perlrun.
Chapter summaryHere's what you know about Perl's operation and syntax so far:
Perl programs typically start with a "shebang" line
statements (generally) end insemicolons
statements may span multiple lines; it's only the semicolon that ends a statement
comments are indicated by a hash (#) sign. Anything after a hash sign on a line is a comment.
\n is used to indicate a newline
whitespace is ignored almosteverywhere
command line arguments to Perl can be indicated on the shebangline
the -w command line argument turns on warnings
Chapter 4. Perl variables
In this chapter...In this section we will explore Perl's three main variable types ---scalars, arrays, and hashes --- and learn to assign values to them,retrieve the values stored in them, and manipulate them in certain ways.
What is a variable?A variable is a place where we can store data. Think of it like apigeonhole with a name on it indicating what data is stored in it.
The Perl language is very much like human languages in many ways, so youcan think of variables as being the "nouns" of Perl. For instance, youmight have a variable called "total" or "employee".
Variable namesVariable names in Perl may contain alphanumeric characters in upper orlower case, and underscores. A variable name may not start with anumber, though - that means something special, which we'll encounterlater. Likewise, variables that start with anything non-alphanumericare also special, and we'll discuss that later, too.
It's standard Perl style to name variables in lower case, withunderscores separating words in the name. For instance,employee_number. Upper case is usually used for constants,for instance LIGHT_SPEED or PI. Following theseconventions will help make your Perl more maintainable and more easilyunderstood by others.
Lastly, variable names all start with a punctuation sign depending onwhat sort of variable they are:
Table 4-1. Variable punctuation
Variable typeStarts withPronounced
Scalar$dollar
Array@at
Hash%Percent
(Don't worry if those variable type names don't mean anything to you. We're about to coverit.)
Variable scoping and the strict pragmaMany programming languages require you to "pre-declare" variables --that is, say that you're going to use them before you use them.Variables can either be declared as global (that is, they can be usedanywhere in the program) or local (they can only be used in the samepart of the program in which they were declared).
In Perl, it is not necessary to declare your variables before youbegin. You can summon a variable into existence simply by using it,and it will be globally available to any routine in your program. Ifyou're used to programming in C or any of a number of other languages,this may seem odd and even dangerous to you. This is, in fact, thecase.
Arguments in favour of strictnessavoids accidental creation of unwanted variables when you make a typing error
avoids scoping problems, for instance when a subroutine uses a variable with the same name as a global variable
allows for warnings if values are assigned to variables and never used
Arguments against strictnesstakes a while to get used to, and may slow down development until it becomes instinctual
enforces a nasty, fascist style of coding which isn't nearly as much fun
Sometimes a little bit of fascism is a good thing, like when you wantthe trains to run on time. Because of this, Perl lets you turnstrictness on if you want it, using something called the strictpragma. A pragma, in Perl-speak, is a set of rules for how your codeis to be dealt with.
Readme: Other effects of the strict pragma are discussed on page500 of the Camel.
Using the strict pragmaIn the interests of bug-free code and teaching better Perl style,we're going to use the strict pragma throughout this training course.Here's how it's invoked:
#!/usr/bin/perl -wuse strict;That typically goes at the top of your program, just under yourshebang line and introductory comments.
Once we use the strict pragma, we have to explicitly declare newvariables using my. You'll see this in use below, and it will be discussed again later when we talk about blocks and subroutines.
Try running the program exercises/strictfail.pl and see whathappens. What needs to be done to fix it? Try it and see if itworks. By the way, get used to this error message - it's one of themost common Perl programming mistakes, though it's easily fixed.
Readme: There's more about use of my on page 189 of the Camel.
ScalarsThe simplest form of variable in Perl is the scalar. A scalar is a single item of data such as:
Arthur
Just Another Perl Hacker
42
0.000001
3.27e17
Here's how we assign values to scalar variables:
my $name = "Arthur";my $whoami = 'Just Another Perl Hacker';my $meaning_of_life = 42;my $number_less_than_1 = 0.000001;my $very_large_number = 3.27e17; # 3.27 by 10 to the power of 17Advanced: There are other ways to assign things apart from the= operator, too. They're covered on pages 92-93 of the Camel.
As you can see, a scalar can be text of any length, and numbers of anyprecision (machine dependent, of course). Perl magically convertsbetween them when it needs to. For instance, it's quite legal to say:
# adding an integer to a floating point numbermy $sum = $meaning_of_life + $number_less_than_1;# here we're putting the int in the middle of a string we# want to printprint "$name says, 'The meaning of life is $meaning_of_life.'\n";This may seem extraordinarily alien to those used to strictly typedlanguages, but believe it or not, the ability to transparently convertbetween variable types is one of the great strengths of Perl. Somepeople say that it's also one of the great weaknesses.
Advanced: You can explicitly cast scalars to various specific data types. Look upint() on page 180 of the camel, for instance.
Double and single quotesWhile we're here, let's look at the assignments above. You'll seethat some have double quotes, some have single quotes, and some haveno quotes at all.
In Perl, quotes are required to distinguish strings from thelanguage's reserved words or other expressions. Either type of quote can be used, but there is one important difference: double quotes can include other variable names inside them, and those variables will thenbe interpolated - as in the last example above - while single quotes donot interpolate.
# single quotes don't interpolate...my $price = '$9.95';# double quotes interpolate...my $invoice_item = "24 widgets at $price each\n";print $invoice_item;
The above example is available in your directory asexercises/interpolate.pl so you can experiment with different kinds of quotes.
Note that special characters such as the \n newline character areonly available within double quotes. Single quotes will fail to expandthese special characters just as they fail to expand variable names.
When using either type of quotes, you must have a matching pair ofopening and closing quotes. If you want to include a quote mark in theactual quoted text, you can escape it by preceding it with a backslash:
print "He said, \"Hello!\"\n";You can also use a backslash to escape other special characters such asdollar signs within double quotes:
print "The price is \$300\n";To include a literal backslash in a double-quoted string, use twobackslashes: \\
Readme: There are special quotes for executing a string as a shell command(see "Input operators" on page 52 of the Camel), and also specialquoting functions (see "Pick your own quotes" on page 41).
ExercisesWrite a script which sets some variables:
your name
your street number
your favourite colour
Print out the values of these variables using double quotes for variable interpolation
Change the quotes to single quotes. Whathappens?
Write a script which prints out C:\WINDOWS\SYSTEM\ twice -- once using double quotes, once using single quotes. How do you have to escape the backslashes in each case?
You'll find answers to the above inexercises/answers/scalars.pl.
ArraysIf you think of a scalar as being a singular thing, arrays are theplural form. Just as you have a flock of sheep or a wunch of bankers,you can have an array of scalars.
An array is a list of (usually related) scalars all kept together.Arrays start with an @ (at sign), and are initialised thus:
my @fruit = ("apples", "oranges", "guavas", "passionfruit", "grapes");my @magic_numbers = (23, 42, 69);my @random_scalars = ("mumble", 123.45, "willy the wombat", -300);As you can see, arrays can contain any kind of scalars. They can havejust about any number of elements, too, without needing to know howmany before you start. Really any number - tens or hundreds of thousands, if you've got the memory.
Readme: Arrays are discussed on page 6 of the Camel or by typingperldoc perldata.
So if we don't know how many items there are in an array, how can wefind out? Well, there are a couple of ways.
First of all, Perl's arrays are indexed from zero. We can accessindividual elements of the array like this:
print $fruits[0]; # prints "apples"print $random_scalars[2]; # prints "willy the wombat"Wait a minute, why are we using dollar signs in the example above,instead of at signs? The reason is this: we only want a scalar back,so we show that we want a scalar. There's a useful way of thinkingof this, which is explained in chapter 1 of the Camel: if scalars arethe singular case, then the dollar sign is like the word "the" -"the name", "the meaning of life", etc. The @ sign on anarray, or the % sign on a hash, is like saying "those" or"these" - "these fruit", "those magic numbers". However, whenwe only want one element of the array, we'll be saying things like"the first fruit" or "the last magic number" - hence thescalar-like dollar sign.
If we wanted what we call an array slice we could say:
@fruits[1,2,3]; # oranges, guavas, passionfruit@magic_numbers[0..1]; # 23, 42You just learnt something new, by the way: the .. ("dotdot") range operator (see pages 90-91 of your Camel or perldocperlop) which creates atemporary list of numbers between the two you specify - in this case 0and 1, but it could have been 1 and 100 if we'd had an array bigenough to use it on. You'll run into this operator again and again,so remember it.
Another thing you can do with arrays is insert them into a string, thesame as for scalars:
print "My favourite fruits are @fruits\n"; # whole arrayprint "Two types of fruit are @fruits[0,2]"; # array sliceReturning to the point, how do we find the last element in an array?Well, there's a special variable called $#array which is theindex of the last element, so you can say:
@fruit[0..$#fruit];and you'll get the whole array. If you print $#fruit you'llfind it's 4, which is not the same as the number of elements - 5.Remember that it's the index of the last element and that theindex starts at zero, so you have to add one to it to find outhow many elements in the array.
But wait! There's More Than One Way To Do It - and an easier way, atthat. If you evaluate the array in a scalar context - that is, dosomething like this:
my $fruit_count = @fruits;
... you'll get the number of elements in the array.
Advanced: There's more than two ways to do it, as well - scalar(@fruits) and int(@fruits) will also tell us how many elements there are in the array.
A quick look at contextThere's a term you've heard used just recently but which hasn't beenexplained: context.
All Perl expressions are evaluated in a context. The two maincontexts are:
scalar context, and
list context
Here's an example of an expression which can be evaluated in eithercontext:
my $howmany = @array; # scalar contextmy @newarray = @array; # list context
If you look at an array in a scalar context, you'll see how manyelements it has; if you look at it in list context, you'll see thecontents of the array itself.
What's the difference between a list and an array?Not much, really. A list is just an unnamed array. Here's ademonstration of the difference:
# printing a list of scalarsprint ("Hello", " ", $name, "\n");# printing an array@hello = ("Hello", " ", $name, "\n");print @hello;If you come across something that wants a LIST, you can either give itthe elements of list as in the first example above, or you can pass itan array by name. If you come across something that wants an ARRAY, youhave to actually give it the name of an array.
Readme: List values and Arrays are covered on page 47 of the Camel.
ExercisesCreate an array of your friends' names
Print out the first element
Print out the last element
Print out the array from within a double-quoted string using variable interpolation
Print out an array slice of the 2nd to 4th items using variable interpolation
Answers to the above can be found inexercises/answers/arrays.pl
Advanced exercisesPrint the array without putting quotes around its name. Whathappens?
Set the special variable $, to something appropriate and try the previous step again (see page 132 of your Camel for this variable's documentation)
What happens if you have a small array and then you assign a value to $array[1000]?
Answers to the above can be found inexercises/answers/arrays_advanced.pl
HashesA hash is a two-dimensional array which contains keys and values.Instead of looking up items in a hash by an array index, you can look upvalues by their keys.
Readme: Hashes are covered in the Camel on pages 7-8, then in moredetail on page 50 or in perldoc perldata.
Initialising a hashHashes are initalised in exactly the same way as arrays, with a commaseparated list of values:
my %monthdays = ("January", 31, "February", 28, "March", 31, ...);Of course, there's more than one way to do it:
my %monthdays = ( "January" => 31, "February" => 28, "March" => 31, ...);