Introduction to Perl

Kirrily Robert

Training co-ordinator
Netizen Pty Ltd

Open Publication License

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 was distributed (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 any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the OC a copy of this License along with the OC. You may at your option charge a fee for the media and/or handling involved in creating a unique copy of the OC for use offline, you may at your option offer instructional support for the OC in exchange for a fee, or you may at your option offer warranty in exchange for a fee. You may not charge a fee for the OC itself. You may not charge a fee for the sole service of providing access to and/or use of the OC via a network (e.g. the Internet), whether it be via the world wide web, FTP, or any other method.

2. You may modify your copy or copies of the OpenContent or any portion of it, thus forming works based on the Content, and distribute such modifications or work under the terms of Section 1 above, 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, the exact 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 is derived from the OC or any part thereof, to be licensed as a whole at no charge to all third parties under 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 are not derived from the OC, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the OC, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Exceptions are made to this requirement to release modified works free 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 else grants you permission to copy, distribute or modify the OC. These actions are prohibited by law if you do not accept this License. Therefore, by distributing or translating the OC, or by deriving works herefrom, you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or translating the OC.

NO WARRANTY

4. BECAUSE THE OPENCONTENT (OC) IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE OC, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE OC "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE OF THE OC IS WITH YOU. SHOULD THE OC PROVE FAULTY, INACCURATE, OR OTHERWISE UNACCEPTABLE YOU ASSUME THE COST OF ALL NECESSARY REPAIR OR CORRECTION.

5. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MIRROR AND/OR REDISTRIBUTE THE OC AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE OC, EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

Additionally:

6. If you offer training based upon this Open Content, you must prominently display a notice stating whether or not you are a Netizen Certified Training Organisation on the Open Content itself and on any material advertising or publicising your training.

a) If you are a Netizen Certified Training Organisation, you must state that you are a Netizen Certified Training Organisation and display the Netizen Certified Training Organisation logo. You must also provide a URL for more information, namely http://netizen.com.au/services/training/ncto/

b) If you are not a Netizen Certified Training Organisation, you must state that you are not a Netizen Certified Training Organisation. You may not use the Netizen Certified Training Organisation logo. You must 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 rights reserved.


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