Programming in Perl (3 days)

Synopsis

This course leads the student from the basics of writing and running Perl scripts to more advanced features such as file operations, report writing, the use of regular expressions, working with binary data files, and writing CGI scripts for web servers.

This is a hands-on programming class. All concepts are reinforced by informal practice during the lecture followed by graduated lab exercises which provide practice in the topics just discussed.

Target audience This course is appropriate for advanced users, system administrators and web site administrators who want to use Perl to support their server installations, as well as anyone else who wants to automate or simplify common tasks with the use of Perl scripts.
Prerequisites

Students should already have a working, user-level knowledge of an operating system such as UNIX or Windows 95. While not mandatory, basic skills with at least one other programming language is desirable.

When presenting this course to a group of non-programmers, we suggest the class length be extended to four days.

Lab exercises

Students will write numerous Perl scripts to reinforce the major concepts covered in this course. The courses will increase in complexity as more sophisticated techniques are introduced.

Exercises will include math, string handling, file I/O, and other real-world topics. As will all our courses, the lab exercises come right after their mention in the lecture. Students do not have to wait until the end of the day to try out what they've just learned.

Course Outline

1. An overview of Perl

  • What is Perl?
  • How to obtain Perl
  • Getting help about Perl

2. Creating and running Perl programs

  • The general structure of a Perl program
  • Running a Perl script under Unix and Windows
  • Implicit/Explicit Execution

3. Basic Data and I/O

  • Perl data types
  • Literals and scalar variables
  • Arithmetic operators and expressions
  • Character strings
  • Keyboard input
  • Removing newlines

4. Logic and Loops

  • The if construct
  • Comparison operators
  • Statement modifiers
  • The while loop
  • The for loop
  • Exiting loops

5. Lists and Arrays

  • The list data type
  • Literal lists
  • Array creation and element access
  • Iterating through an array
  • The range operator

6. Reading and Writing Text Files

  • Opening and closing files
  • Reading Text
  • Writing Text
  • Using < >
  • Writing UNIX-style filters

7. List Functions

  • Adding and deleting array elements
  • Using split( ) and join( )
  • Sorting
  • Using reverse( )

8. Formatting Output

  • Using printf( )
  • Creating report formats
  • The write() function
  • Using multiple formats

9. Hashes

  • About hashes
  • Creating and accessing
  • Testing for existence
  • Traversing hashes

10. References

  • Creating references
  • Dereferencing
  • Multidimensional data structures
  • Anonymous data

11. Working with Text

  • String functions
  • Matching and substitution
  • String translation
  • Regular expressions
  • The basic RE metacharacters
  • Quantifiers
  • Anchors
  • Modifiers
  • Using subexpressions

12. Raw File and Data Access

  • Opening files in raw mode
  • Reading raw data
  • Writing raw data
  • Translating data with pack() and unpack()

13. Subroutines

  • Creating subroutines
  • Calling subroutines
  • Parameters and return values
  • Making variables private
  • Using modules

14. Working with the OS

  • Environment variables
  • Running external programs
  • Deleting and renaming files
  • File test operators
  • User Identification
  • Time of day

15. CGI scripts and the World Wide Web

  • An overview of CGI
  • Using CGI.pm
  • Creating HTML forms
  • Parsing the CGI data stream
  • CGI environment variables
  • Saving parameters
  • Outputting HTTP header & web content
  • Generating HTML from templates