Introduction to Perl Programming Introduction to PERL Programming: a Stroll Through Perl from "Learning Perl", 2nd ed. by Randal L. Schwartz Nancy McCracken NPAC Syracuse University 111 College Place Syracuse NY 13244-4100 September 5, 1997 Click here for body text PERL4 Text: Learning PERL (the Llama book), 2nd ed., Randal L. Schwartz, OšReilly & Associates, 1997. PERL4 is an interpreted language that can be regarded as a cross between C, Unix shell, sed and awk. It is a C-based language which can also deal directly with Unix commands and file system and easily do string processing matching. In this course, we will concentrate not on using PERL in systems programming, but in using PERL for CGI programming, i.e. implementing programs activated from Web pages. In general, we use PERL for tedious high level things which can take a long time to program but not much execution time. For computationally intense programs, we would use a compiled language such as C. Our first lecture on Perl will show a series of small programming examples from Chapter 1 of the Learning Perl book, designed to illustrate the main features of the language. Later we will cover each topic in more detail. The Simplest Program: Hello, World! Each Perl program is in a Unix file, with executable permission, and is executed by typing the name of the file as a command. Scalar Variables and Keyboard Input This program uses a scalar variable, which always starts with a $, prompts for input, reads the name, and prints hello. Conditional and Comparison This program prints a special greeting for Randal, and an ordinary one for everybody else. While Loop This program asks everyone, except for Randal, for a secret password. Arrays, also known as lists This program uses an array variable, which must start with @, to hold several secret passwords. Associative arrays Associative arrays, variables start with %, are tables with keywords and values and are used here to give each person their own password. Regular Expressions and String Matching Accept input name in varying formats by matching strings that are case insensitive and longer, such as Randal L. Schwartz Substitute and Translate Operators Truncate all input names to the first name and translate to all lower case. Subroutines All subroutines in Perl have parameters and return a result, the value of the last expression before the return. Undeclared names refer to names in the outer program (global). The subroutine is called with its parameters, for example good_word($name,$guess); Reading Files A file (or I/O channel) is referred to by a "filehandle". (Perl automatically creates filehandles STDIN, STDOUT and STDERR.) We choose to keep our password table in a file with one word per line, alternating keys and values. Sending commands to the operating system If the wrong person guesses a password, we can send a mail message by issuing the mail command. The pipe symbol is used in the file name to indicate a command. Formatting output and using file names A format statement can be associated with each file handle and used for writing to that channel. The format name is the name of the file handle - there can can also be another special format for labelling the tops of output pages. Further Topics Further examples in Chapter 1 of Learning Pearl discuss renaming files keeping an associative table in a file database Final programs are on page 28.