Perl introduction

Perl stands for "Practical Extraction and Report Language." It accounts for over 50% of the CGIs written on the Web and can be used on Unix, Windows 9x, 2000 and NT. I strongly suggest that you read the book Learning Perl in Windows or Unix/Linux , if you are interested in learning Perl in more details. You can also find a Perl Manual here. The main Perl support page is at www.perl.com. Perl has also been ported to other systems like Macintosh, OS/2, etc. Read the Availability FAQ for more information.

The mis.ubalt.edu server uses Perl 5, and also most of the UB servers. Perl 5 is also installed at the UB open labs under Windows NT. You do NOT need to install it at the labs. At the MIS Lab both NT and Linux have Perl 5 installed. Remotely, download the current version for Windows from ActiveWare. If you have Linux installed home it already has Perl 5 installed as part of most distributions.


Perl syntax overview


Introduction

Comments in Perl are marked with the # (number) sign and run to the end of the line. All statement lines end with ; (semicolon). There are three types of variables in Perl -- scalar ($), array (@) and associative array (%). Perl supports a variety of control structures -- block statements, if/else, unless, while/until, for, foreach (no goto is really supported). Perl provides easy I/O statements -- STDIN, STDOUT, print/printf, open/close (filehandles), die, format, as well as directory access. Finally, functions/subroutines are also easy to create and use. A variety of Perl libraries are available for simplifying many of the Web tasks.

Variables

  1. Scalar variables. Scalar variables can be numeric or strings and start with a $ sign, as in $name. They contain one single value like 34.5 or "Have a nice day!". Please note that strings are enclosed in quotes. There are two types of quotes used 'single' and "double" . I recommend that beginners use double-quotes for they allow interpolation (translation) of variable-names and allows \ (backslash) to specify escape characters. For example Print "This is my $name \n"; will print the quoted text with the value of $name replaced by its value, followed by a new line. See example 1
  2. Array variables. Array variables are an ordered list of scalar data and start with @ sign, as in @lots. Each element is an individual variable with a scalar value, separated by commas, as in @a = ($a, 17, "nice day"); Subscripts start from 0 (zero), so $a [1] has the value 17. Please note that $a and $a[1] are not the same variables. For $a [1] is the scalar value of the @a array variable first element, while $a is a simple scalar variable. See example 2
  3. Associative arrays. Associative array variables (or hashes) are an unordered list of pairs of scalar keys and values, separated by commas and start with a % sign, as in %a. As in arrays we use associative arrays through their scalar values, like $a{age} = 34; Meaning that we are assigning the value 34 to be paired with the key age (we may also be creating the key in this statement). Please note that we are using curly braces { instead of brackets [ to identify each element of the list of the associative array. See example 3
  4. Some scalar operators. There are too many operators in Perl for me to summarize here. The traditional +, -, *, /, **, <,>, <=, >=. ==. != operators work for numeric variables, while string variables use lt, gt, le, ge, eq, ne for comparisons, and can be concatenated with . (dot). The logical end is represented by && , the logical or by | |, and the logical not by ! ~ . Autoincrement and autodecrement are represented by ++ and -- , respectively, like in ++$a; meaning $a=$a+1; Binary assignment operators are also supported like in $a+=5; and $a*=3; (adds 5 to $a and assigns result to $a, multiply $a by 3 and assigns the result to $a). See example 4


This page is maintained by Al Bento who can be reached at abento@ubalt.edu. This page was last updated on September 15, 2004. Although we will attempt to keep this information accurate, we can not guarantee the accuracy of the information provided.