Beginning Perl

Introduction

The tutorial presented on the following pages is a revised version of a tutorial I presented twice at a previous job. We held class one hour a week during lunch hour over a 14-week period. The students, who were all scientists, were asked to complete the the weekly homework assignments, and later in the course the students applied their new skills to projects related to their work.

I am still working on the revision, so some of the lessons are incomplete.

Outline

  1. Lesson 1
  2. Lesson 2
  3. Lesson 3
    1. Perl scalar variables
    2. The pragma use warnings;
    3. Getting user input using <>
    4. Using chomp() to remove newline characters
    5. Branching with if, elsif, and else
    6. Operators for testing values
    7. Homework assignment
  4. Lesson 4
    1. Looping using while
    2. True or false
    3. The && and || operators
    4. Homework assignment
  5. Lesson 5
    1. Stopping a script with the die() function
    2. Reading files
      1. Opening a file
      2. Reading lines
      3. Closing the open file
      4. Example script
    3. The ! (not) operator
    4. Writing files
    5. The STDIN, STDOUT, and STDERR filehandles
    6. Homework assignment
  6. Lesson 6
    1. Numbers
      1. Designating numbers
      2. Math operators
        1. Adding with the + operator
        2. Subtracting with the - operator
        3. Multiplying with the * operator
        4. Dividing with the / operator
        5. Finding the remainder with the % (modulo) operator
        6. Calculating a power with the ** (exponentiation) operator
        7. Combining math operators with the = (assignment) operator
        8. Incrementing and decrementing numbers
      3. Math functions
        1. Finding the square root of a number
        2. Finding the natural logarithm of a number
        3. Getting a random number
    2. Strings
      1. Double quote strings
      2. Single quote strings
      3. Concatenating strings with the . operator
      4. Getting a substring with the substr() function
    3. Printing formatted numbers and strings
      1. Using printf()
      2. Using sprintf()
      3. Formatting integers with %d
      4. Formatting floating point numbers with %f
      5. Formatting strings with %f
      6. Formatting percent symbols with %%
    4. Homework assignment
  7. Lesson 7
    1. Lists and arrays
      1. Lists
      2. Assigning lists to scalar variables
      3. Array variables
      4. Using scalar() to find the number of elements in an array
      5. Using individual elements of an array
      6. Using more than one element from an array
      7. Initializing individual elements of an array
      8. Adding elements to or removing elements from the ends of an array
    2. Sorting lists and arrays using sort()
      1. Sorting an array alphabetically
      2. Sorting an array numerically
    3. Looping using foreach
    4. The pragma use warnings; revisited
    5. A summary example script
    6. Homework assignment
  8. Lesson 8
    1. Hash variables
    2. A note about code style
    3. A summary example script
    4. Homework assignment
  9. Lesson 9
    1. How the shell passes commands to a Perl script
    2. Getting the name of the script from within the script
    3. Getting command line arguments from @ARGV
      1. Checking the number of arguments
      2. Using one or more arguments
      3. Providing a help message to the user
    4. Printing error messages to STDERR
    5. A summary example script
    6. Homework assignment
  10. Lesson 10
  11. Lesson 11
  12. Lesson 12
    1. Using Perl’s pseudorandom number generator
    2. Subroutines
      1. Variables inside subroutines
      2. Returning results from a subroutine
        1. Returning nothing
        2. Returning a scalar
        3. Returning an array
        4. Returning a hash
        5. Returning mixed variable types
        6. A combined example
      3. Getting arguments from the @_ variable
    3. Revised structure of scripts
      1. Adding the use strict; line
      2. Marking off main code
      3. Placing subroutines after main code
      4. An example
    4. Summary example script
    5. Homework assignment
  13. Lesson 13
  14. Lesson 14
    1. Perl idioms
      1. The $_ variable
        1. $_ and reading lines from a file
        2. $_ as the default argument for the print() function
        3. $_ as the default argument for the chomp() function
        4. $_ as the default operand for the m// regular expression operator
        5. $_ in a foreach loop
        6. Obscure uses of $_
      2. Using || as a short-circuit
      3. unless, the reverse of if
    2. Obfuscated Perl
  15. Lesson 15
    1. Steps for working on a script someone has given you
      1. Work on a copy of the original script
      2. Use your copy of Programming Perl
      3. Add comments
      4. Save versions of your changes
      5. Give variables meaningful names
      6. Add use warnings;
      7. Add use strict;
      8. Test the script with a small data set
      9. Break the code into manageable subunits
      10. Experiment with code fragments
      11. Add print() statements
      12. Step through the code with the Perl debugger
      13. Consult your local Perl expert