Hands-On Introduction to Perl
Length: 2-3 days
Prerequisites: Students should have at least one year of experience in programming
some general purpose, procedural language, such as Fortran, Basic,
Lisp, ksh, sh, Pascal, C, C++, etc. Students should
also have basic familiarity withthe Unix environment, including how to
use an editor program to create files. Attendees need not have
experience with Perl.
Description
Introductory Perl training is usually structured as a sequence of
lectures. After a 45-minute lecture, the class does lab exercises for
another 45 minutes or so. These are several problems with this. The
biggest one is that nobody likes to sit through a long lecture, and
most people can't pay attention to a long lecture, especially after
lunch.
No class has students that all work at the same pace. Either the
faster students get bored during the lecture, or the slower students
get lost. During the exercises, the same thing happens. The fast
students finish the exercises right away and are left with nothing to
do; the slow students don't have enough time to work on the exercises.
Hands-On Perl provides a solution to these problems. Instead of
alternating lectures and labs, the lecture runs continuously. Each
slide has an example on it. Students try out the example while the
lecture is going on. The lecturer suggests changes to the example, and
the students try out the changes and see the effects immediately. Some
slides have exercises. Students work the exercises while the lecture
is going on. Occasionally the instructor will pause to let the class
finish trying an example.
The dozens of examples are central to the class. Rather than having a
'strings' unit, and showing all of Perl's multitude of string
functions one after the other, as many introductory classes do, this
class immediately starts with simple example programs that deal with
strings. As the class progresses, the examples become more elaborate,
and new string functions are introduced as they are needed to solve
specific problems. All of Perl's major features, including arrays,
hashes, regexes, and subroutines, are treated the same way. Students
remember things better when they have a context into which they can
place the new information. More sophisticated uses of these features
build on the simpler examples from the earlier sections that the
students have already assimilated.
Students find it easier to pay attention and engage the material when
they are involved in what's going on instead of passively absorbing a
lecture. Advanced students try the examples quickly, but a natural
path of exploration presents itself and the student continues to
develop the example until it's time to move on to the next
slide---usually less than two minutes.
Since the slide set is loaded on the students' workstations, any
student can return to an old slide or skip ahead without disturbing
the others.
Outline
- I. Overview of Perl: Filters, Scalars, and Regexes
- What's Perl?
- What's good about Perl?
- Let's run Perl
- What's that \n?
- What Version Do We Have?
- Programs in Files
- Programs on the Command Line: -l and -e
- Simple example: Add a column of numbers
- What's going on here? Scalar variables; <>; while
- Perl is Magical: +=; automatic type conversions; automatic initializations
- print; Interpolation
- String-to-number conversion
- Warnings; -w; the manual; perldiag
- Detecting Malformed Numbers; -n
- Pattern Matching: Very simple patterns; not-so-simple patterns; anchors
- Detecting Malformed Numbers with patterns; \d
- Diagnosing Errors; warn
- Fatal Errors; die
- File Input via <>
- A Better Validator; $.; \t; unless
- Lab 1: Writing simple filters; diagnosing errors
- II. Arrays and Files
- Separating Fields: split; list assignment
- Arrays
- for and foreach loops
- Various array operations: reverse, push, pop, shift, unshift, sort; perlfunc manual
- Command-line arguments: @ARGV; $0
- Skipping Loop Iterations: next; statement labels
- Building an Array with push
- join
- Negative array indices; $#array
- How many elements in an array? Scalar context
- A useful utility: grep
- File Operations: open; filehandles; file copy utility
- More about open: good error diagnosis with $!; printing to filehandles
- Lab 2: Operating on files
- III. Hashes
- A very simple database: Idea
- The very simple database: Detailed code example
- eq vs ==: why?
- Case-insensitive comparisons: uc and lc
- Hashes
- Report generation
- keys
- Deposits and withdrawals
- printf and sprintf for formatting numerals and strings; rounding
- Sorting; sorting numbers; sorting by bank balance; case-insensitive sorting
- File splitting; an improvement
- Random numbers; getting random line from a file
- Lab 3: Interactive query applications; report generation
- IV. Subroutines and Subtle Features
- Pattern substitution; data cleanup
- In-place editing: -i; -p;
- Removing Comments; skipping comments; \S and \s
- Statement modifiers; until
- Extended pattern matching example: a duplicate word detector; \w; \W; +: (...); \1; $&; \b; /x
- Slurping; $/; undef
- A better duplicate word detector: /g; $1; pos
- Subroutines; @_; my; return; .
- Recursive Subroutines; my again
- Subroutine arguments: shift revisited
- Pass-by reference: beware!
- Context; examples; beware!; scalar; context traps
- Building documentation: POD; where to find the manuals; how to read them
- More functions for hashes: values; each; initialization; list assignment
- More about hashes: =>; qw; delete; exists
- Date and Time Functions: time; 'stat; comparing times; localtime; time computation modules including Time::Local
- Lab 4: Time computations; unit testing subroutines; default arguments
- V. Data Structures and Databases (3 day version only)
- References: The city-country problem
- Making References; \; [...]; {...}
- Using References: @ and %; ->
- Two-dimensional arrays
- City-country problem solved; autovivification
- DBI: modules; use
- Tiny overview of SQL: insert; update; delete; select
- Connecting: connect; username and password; RaiseError
- Sending queries with DBI: prepare; statement handles; execute; finish
- Retrieving data with DBI: fetchrow_hashref
- Parameters in Queries: placeholders; ?; do not interpolate!
- Complete DBI example: Author works database
- Lab 5: Mail log analysis
- VI. Filling in the Gaps
- More operators for numbers
- More operators for strings
- More operators for booleans
- More about strings
- More Syntax; the c-style for loop
- More useful functions for scalars
- The Perl debugger
- More useful functions for arrays; splice
- More file functions
- Running external commands
- CPAN
- Network communications
- More about dates and times
- Object-Oriented Programming
- (No lab)
Example Code
I have made all the example code from the
class available online.
Return to:
Universe of Discourse main page |
Perl Paraphernalia |
Other Classes and Talks
mjd-perl-yak+@plover.com