If you have had previous programming experience in another language, such as Visual Basic, C/C++, Java, ASP, or PHP, and you are familiar with basic concepts, such as variables, loops, conditional statements, and functions, Table 2.1 will give you a quick overview of the constructs and syntax of the Perl language.
The Script File | A Perl script is created in a text editor. Normally, there is no special extension required in the filename, unless specified by the application running the script; e.g., if running under Apache as a cgi program, the filename may be required to have a .pl or .cgi extension. | |
Free Form | Perl is a free-form language. Statements must be terminated with a semicolon but can be anywhere on the line and span multiple lines. | |
Comments | Perl comments are preceded by a # sign. They are ignored by the interpreter. They can be anywhere on the line and span only one line. | |
Example
print "Hello, world"; # This is a comment # And this is a comment | ||
Printing Output | The print and printf functions are built-in functions used to display output.
The print function arguments consist of a comma-separated list of strings and/or numbers. The printf function is similar to the C printf() function and is used for formatting output. Parentheses are not required around the argument list. (See Chapter 3.)print value, value, value; printf ( string format [, mixed args [, mixed ...]] ); | |
Example
print "Hello, world\n";
print "Hello,", " world\n";
print ("It's such a perfect day!\n"); #Parens optional;.
print "The the date and time are: ", localtime, "\n";
printf "Hello, world\n";
printf("Meet %s%:Age 5d%:Salary \$10.2f\n", "John", 40, 55000); (See Chapter 4.) | ||
Data Types/Variables | Perl supports three basic data types to hold variables: scalars, arrays, and associative arrays (hashes).
Perl variables don't have to be declared before being used. Variable names start with a "funny character," followed by a letter and any number of alphanumeric characters, including the underscore. The funny character represents the data type and context. The characters following the funny symbol are case sensitive. If a variable name starts with a letter, it may consist of any number of letters (an underscore counts as a letter) and/or digits. If the variable does not start with a letter, it must consist of only one character. (See Chapter 5.) | |
Scalar | A scalar is a variable that holds a single value, a single string, or a number. The name of the scalar is preceded by a "$" sign. Scalar context means that one value is being used. | |
Example
$first_name = "Melanie"; $last_name = "Quigley"; $salary = 125000.00; print $first_name, $last_name, $salary; | ||
Array | An array is an ordered list of scalars; i.e., strings and/or numbers. The elements of the array are indexed by integers starting at 0. The name of the array is preceeded by an "@" sign.
@names = ( "Jessica", "Michelle", "Linda" ); print "@names"; #Prints the array with elements separated by a space print "$names[0] and $names[2]"; #Prints "Jessica" and "Linda" print "$names[-1]\n"; # Prints "Linda" $names[3]="Nicole"; #Assign a new value as the 4th element Some commonly used built-in functions: | |
pop | removes last element | |
push | adds new elements to the end of the array | |
shift | removes first element | |
unshift | adds new elements to the beginning of the array | |
splice | removes or adds elements from some position in the array | |
sort | sorts the elements of an array | |
Hash | An associative array, called a hash, is an unordered list of key/value pairs, indexed by strings. The name of the hash is preceded by a "%" symbol. (The % is not evaluated when enclosed in either single or double quotes.) | |
Example
%employee = ( "Name" => "Jessica Savage", "Phone" => "(925) 555-1274", "Position" => "CEO" ); print "$employee{"Name"}; # Print a value $employee{"SSN"}="999-333-2345"; # Assign a key/value Some commonly used built-in functions: | ||
keys | retrieves all the keys in a hash | |
values | retrieves all the values in a hash | |
each | retrieves a key/value pair from a hash | |
delete | removes a key/value pair | |
Predefined Variables | Perl provides a large number of predefined variables. The following is a list of some common predefined variables: | |
$_ | The default input and pattern-searching space. | |
$. | Current line number for the last filehandle accessed. | |
$@ | The Perl syntax error message from the last eval() operator. | |
$! | Yields the current value of the error message, used with die. | |
$0 | Contains the name of the program being executed. | |
$$ | The process number of the Perl running this script. | |
$PERL_VERSION / $^V | The revision, version, and subversion of the Perl interpreter. | |
@ARGV | Contains the command-line arguments. | |
ARGV | A special filehandle that iterates over command-line filenames in @ARGV. | |
@INC | Search path for libarary files. | |
@_ | Within a subroutine the array @_ contains the parameters passed to that subroutine. | |
%ENV | The hash %ENV contains your current environment. | |
%SIG | The hash %SIG contains signal handlers for signals. | |
Constants (Literals) | A constant value, once set, cannot be modified. An example of a constant is PI or the number of feet in a mile. It doesn't change. Constants are defined with the constant pragma as shown here. | |
Example
use constant BUFFER_SIZE => 4096;
use constant PI => 4 * atan2 1, 1;
use constant DEBUGGING => 0;
use contstant ISBN => "0-13-028251-0";
PI=6; # Cannot modify PI; produces an error. | ||
Numbers | Perl supports integers (decimal, octal, hexadecimal), floating point numbers, scientific notation, Booleans, and null. | |
Example
$year = 2006; # integer $mode = 0775; # octal number in base 8 $product_price = 29.95; #floating point number in base 10 $favorite_color = 0x33CC99; #integer in base 16 (hexadecimal) $distance_to_moon=3.844e+5; #floating point in scientific notation $bits = 0b10110110; # binary number | ||
Strings and Quotes | A string is a sequence of bytes (characters) enclosed in quotes.
When quoting strings, make sure the quotes are matched; e.g., "string" or 'string'. Scalar and array variables ($x, @name) and backslash sequences (\n, \t, \", etc.) are interpreted within double quotes; a backslash will escape a quotation mark, a single quote can be embedded in a set of double quotes, and a double quote can be embedded in a set of single quotes. A here document is a block of text embedded between user-defined tags, the first tag preceded by <<. The following shows three ways to quote a string:
| |
Example
$question = 'He asked her if she wouldn\'t mind going to Spain'; # Single quotes $answer = 'She said: "No, but it rains in Spain."'; # Single quotes $line = "\tHe said he wouldn't take her to Spain\n"; $temperature = "78"; print "It is currently $temperature degrees"; #Prints: "It is currently 78 degrees.". Variables are #interpreted when enclosed in double quotes, but not # single quotes | ||
Alternative Quotes | Perl provides an alternative form of quoting. The string to be quoted is delimted by a nonalphanumeric character or characters that can be paired, such as ( ), { }, [ ]. The constructs are: qq, q, qw, qx | |
Example
print qq/Hello\n/; # same as: print "Hello\n"; print q/He owes $5.00/, \n"; # same as: print 'He owes $5.00', "\n"; @states=qw( ME MT CA FL ); #same as ("ME", "MT", "CA","FL") $today = qx(date); # same as $today = 'date'; | ||
Operators | Perl offers many types of operators, but for the most part they are the same as C/C++/Java or PHP operators. Types of operators are (see Chapter 6): | |
Assignment | =, +=, -=, *= , %=, ^=, &=, |=, .= | |
Numeric equality | = =, !=, <=> | |
String equality | eq, ne, cmp | |
Relational numeric | > >= < <= | |
Relational string | gt, ge, lt, le | |
Range | 5 .. 10 # range between 5 and 10, increment by 1 | |
Logical | &&, and, ||, or, XOR, xor, ! | |
Autoincrement/decrement | ++ -- | |
File | -r, -w, -x,-o, -e, -z, -s, -f, -d, -l, etc. | |
Bitwise | ~ & | ^ << >> | |
String concatenation | . | |
String repetition | x | |
Arithmetic | * / - + % | |
Pattern matching | =~, !~ | |
Example
Code View: print "\nArithmetic Operators\n"; print ((3+2) * (5-3)/2); print "\nString Operators\n"; # Concatenation print "\tTommy" . ' ' . "Savage"; print "\nComparison Operators\n"; print 5>=3 , "\n"; print 47==23 , "\n"; print "\nLogical Operators\n"; $a > $b && $b < 100 $answer eq "yes" || $money == 200 print "\nCombined Assignment Operators\n"; $a = 47; $a += 3; # short for $a = $a + 3 $a++; # autoincrement print $a; # Prints 51 print "\nPattern Matching Operators\n" $color = "green"; print if $color =~ /^gr/; #$color matches a pattern # starting with 'gr' $answer = "Yes"; print if $answer !~ /[Yy]/; #$answer matches a pattern # containing 'Y' or 'y' | ||
Conditionals | The basic if construct evaluates an expression enclosed in parentheses, and if the condition evaluates to true, the block following the expression is executed. (See Chapter 7.) | |
if statement | if ( expression ){ statements; } | |
Example
if ( $a == $b ){ print "$a is equal to $b"; } | ||
if/else statement | The if/else block is a two-way decision. If the expression after the if condition is true, the block of statements is executed; if false, the else block of statements is executed.
if ( expression ){ statements; else{ statements; } | |
Example
$coin_toss = int (rand(2 )) + 1; # Generate a random # number between 1 and 2 if( $coin_toss == 1 ) { print "You tossed HEAD\n"; } else { print "You tossed TAIL\n"; } | ||
if/elsif statement | The if/elsif/else offers multiway branch; if the expression following the if is not true, each of the elsif expressions is evaluated until one is true; otherwise, the optional else statements are executed.
if ( expression ){ statements; elsif ( expression ){ statements; } elsif (expression){ statements; else{ statements; } | |
Example
# 1 is Monday, 7 Sunday
$day_of_week = int(rand(7)) + 1;
print "Today is: $day_of_week\n";
if ( $day_of_week >=1 && $day_of_week <=4 ) {
print "Business hours are from 9 am to 9 pm\n";
}
elsif ( $day_of_week == 5) {
print "Business hours are from 9 am to 6 pm\n";
}
else {
print "We are closed on weekends\n";
} | ||
Conditional Operator | Like C/C++, Perl also offers a shortform of the if/else syntax, which uses three operands and two operators (also called the ternary operator). The question mark is followed by a statement that is executed if the condition being tested is true, and the colon is followed by a statement that is executed if the condition is false. (condition) ? statement_if_true : statement_if_false; | |
Example
$coin_toss = int (rand(2 )) + 1; #Generate a random number # between 1 and 2 print ($coin_toss == 1 ? "You tossed HEAD\n" : "You tossed TAIL\n" ); | ||
Loops | A loop is a way to specify a piece of code that repeats many times. Perl supports several types of loops: the while loop, do-while loop, for loop, and foreach loop. (See Chapter 7.) | |
while/until Loop | The while loop:
The while is followed by an expression enclosed in parentheses, and a block of statements. As long as the expression tests true, the loop continues to iterate.
while ( conditional expression ) { code block A } | |
Example
$count=0; # Initial value while ($count < 10 ){ # Test print $n; $count++; # Increment value } | ||
The until loop:
The until is followed by an expression enclosed in parentheses, and a block of statements. As long as the expression tests false, the loop continues to iterate.
until ( conditional expression ) { code block A } | ||
Example
$count=0; # Initial value until ($count == 10 ){ # Test print $n; $count++; # Increment value } | ||
do-while Loop | The do-while loop:
The do-while loop is similar to the while loop except it checks its looping expresssion at the end of the loop block rather than at the beginning, guaranteeing that the loop block is executed at least once.
do { code block A } while (expression); | |
Example
$count=0; # Initial value do { print "$n "; $count++; # Increment value while ($count < 10 ); # Test } | ||
for Loop | The for loop:
The for loop has three expressions to evaluate, each separated by a semicolon. The first inititializes a variable and is evaluated only once. The second tests whether the value is true, and if it is true, the block is entered; if not, the loop exits. After the block of statements is executed, control returns to the third expression, which changes the value of the variable being tested. The second expression is tested again, etc.
for( initialization; conditional expression; increment/decrement ) { block of code } | |
Example
for( $count = 0; $count < 10; $count = $count + 1 ) {
print "$count\n";
} | ||
foreach Loop | The foreach loop:
The foreach is used only to iterate through a list, one item at a time.
foreach $item ( @list ) { print $item,"\n"; } | |
Example
@dessert = ( "ice cream", "cake", "pudding", "fruit"); foreach $choice (@dessert){ # Iterates through each element of the array echo "Dessert choice is: $choice\n"; } | ||
Loop Control | The last statement is used to break out of a loop from within the loop block. The next statement is used to skip over the remaining statements within the loop block and start back at the top of the loop. | |
Example
$n=0; while( $n < 10 ){ print $n; if ($n == 3){ last; # Break out of loop } $n++; } print "Out of the loop.<br>"; Example for($n=0; $n<10; $n++){ if ($n == 3){ next; # Start at top of loop; # skip remaining statements in block } echo "\$n = $n<br>"; } print "Out of the loop.<br>"; | ||
Subroutines/Functions | A function is a block of code that peforms a task and can be invoked from another part of the program. Data can be passed to the function via arguments. A function may or may not return a value. Any valid Perl code can make up the definition block of a function.
Variables outside the function are available inside the function. The my function will make the specified variables local. (See Chapter 11.) sub function_name{block of code } | |
Example
sub greetings() { print "Welcome to Perl!<br>"; #Function definition } &greetings; # Function call greetings(); # Function call Example $my_year = 2000; if ( is_leap_year( $my_year ) ) { #Call function with an argument print "$my_year is a leap year\n"; } else { print "$my_year is not a leap year"; } sub is_leap_year { # Function definition my $year = shift(@_); # Shift off the year from # the parameter list, @_ return ((($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0)) ? 1 : 0; #What is returned from the function } | ||
Files | Perl provides the open function to open files, and pipes for reading, writing, and appending. The open function takes a user-defined filehandle (normally a few uppercase characters) as its first argument and a string containing the symbol for read/write/append followed by the real path to the system file. (See Chapter 10.) | |
Example
open(FH, "<filename"); #Opens "filename" for reading. # The < symbol is optional. open (DB, "/home/ellie/myfile") or die "Can't open file: $!\n"; open(FH, ">filename"); #Opens "filename" for writing. #Creates or truncates file. To open a file for appending: open(FH, ">>filename"); #Opens "filename" for appending. #Creates or appends to file. To open a file for reading and writing: open(FH, "+<filename"); #Opens "filename" for read, then write. open(FH, "+>filename"); #Opens "filename" for write, then read. close(FH); while(<FH>){ print; } #Read one line at a time from file. @lines = <FH>; #Slurp all lines into an array. print "@lines\n"; To write to a file: open(FH, ">file") or die "Can't open file: $!\n"; print FH "This line is written to the file just opened.\n"; print FH "And this line is also written to the file just opened.\n"; Example To Test File Attributesprint "File is readable, writeable, and executable\n" if -r $file and -w _ and -x _; #Is it readble, writeable, and executable? print "File was last modified ",-M $file, " days ago.\n"; # When was it last modified? print "File is a directory.\n " if -d $file; # Is it a directory? | ||
Pipes | Pipes can be used to send the output from system commands as input to Perl and to send Perl's output as input to a system command. To create a pipe, also called a filter, the open system call is used. It takes two arguments: a user-defined handle and the operating system command, either preceded or appended with the "|" symbol. If the command is preceded with a "|", the operating system command reads Perl output. If the command is appended with the "|" symbol, Perl reads from the pipe; if the command is prepended with "|", Perl writes to the pipe. (See Chapter 10.) | |
Example
Input filter open(F, " ls |") or die; # Open a pipe to read from while(<F>){ print ; } # Prints list of UNIX files Output filer open(SORT, "| sort" ) or die; # Open pipe to write to print SORT "dogs\ncats\nbirds\n" # Sorts birds, cats, dogs on separate lines. |
At the end of each section, you will be given the chapter number that describes the particular construct and a short, fully functional Perl example designed to illustrate how that constuct is used.
If you are not familiar with programming, skip this chapter and go to Chapter 5. You may want to refer to this chapter later for a quick reference.
A regular expression is set of characters enclosed in forward slashes. They are to match patterns in text and to refine searches and substitutions. Perl is best known for its pattern matching. (See Chapter 8.)
Code View: $_ = "looking for a needle in a haystack"; print if /needle/; If $_contains needle, the string is printed. $_ = "looking for a needle in a haystack"; # Using regular expression metacharacters print if /^[Nn]..dle/; # characters and "dle". $str = "I am feeling blue, blue, blue..." $str =~ s/blue/upbeat/; # Substitute first occurrence of "blue" with "upbeat" print $str; I am feeling upbeat, blue, blue... $str="I am feeling BLue, BLUE..."; $str = ~ s/blue/upbeat/ig; # Ignore case, global substitution print $str; I am feeling upbeat, upbeat... $str = "Peace and War"; $str =~ s/(Peace) and (War)/$2, $1/i; # $1 gets 'Peace', $2 gets' War' print $str; War and Peace. $str = "He gave me 5 dollars.\n" s/5/6*7/e; # Rather than string substitution, evaluate replacement side print $str; He gave me 42 dollars." |
The @ARGV array is used to hold command-line arguments. If the ARGV filehandle is used, the arguments are treated as files; otherwise, arugments are strings coming in from the command line to be used in a script. (See Chapter 10.)
$ perlscript filea fileb filec (In Script) print "@ARGV\n"; # lists arguments: filea fileb filec while(<ARGV>){ # filehandle ARGV -- arguments treated as files print; # Print each line of every file listed in @ARGV } |
Perl references are also called pointers. A pointer is a scalar variable that contains the address of another variable. To create a pointer, the backslash operator is used. (See Chapter 13.)
Code View: # Create variables $age = 25; @siblings = qw("Nick", "Chet", "Susan","Dolly"); %home = ("owner" => "Bank of America", "price" => "negotiable", "style" => "Saltbox", ); # Create pointer $pointer1 = \$age; # Create pointer to scalar $pointer2 = \@siblings; # Create pointer to array $pointer3 = \%home; # Create pointer to hash $pointer4 = [ qw(red yellow blue green) ]; # Create anonymous array $pointer5 = { "Me" => "Maine", "Mt" => "Montana", "Fl" => "Florida" }; # Create anonymous hash # Dereference pointer print $$pointer1; # Dereference pointer to scalar; prints: 25 print @$pointer2; # Dereference pointer to array; # prints: Nick Chet Susan Dolly print %$pointer3; # Dereference pointer to hash; # prints: styleSaltboxpricenegotiableownerBank of America print $pointer2->[1]; # prints "Chet" print $pointer3->{"style"}; # prints "Saltbox" print @{$pointer4}; # prints elements of anonymous array |
Perl supports objects, a special type of variable. A Perl class is a package containing a collection of variables and functions, called properties and methods. There is no "class" keyword. The properties (also called attributes) are variables used to describe the object. Methods are special functions that allow you to create and manipulate the object. Objects are created with the bless function. (See Chapter 14.)
Code View: package Pet sub new{ # Constructor my $class = shift; my $pet = { "Name" => undef, "Owner" => undef, "Type" => undef, }; bless($pet, $class); # Returns a pointer to the object sub set_pet{ # Accessor methods my $self = shift; my ($name, $owner, $type)= @_; $self->{'Name'} = $name; $self->{'Owner'}= $owner; $self->{'Type'}= $type; } sub get_pet{ my $self = shift; while(($key,$value)=each($%self)){ print "$key: $value\n"; } } |
$cat = Pet->new(); # alternative form is: $cat = new Pet(); # Create an object with a constructor method $cat->set_pet("Sneaky", "Mr. Jones", "Siamese"); # Access the object with an instance $cat->get_pet; |
Perl also supports method inheritance by placing base classes in the @ISA array.
Library files have a .pl extenson; modules have a .pm extension. Today, .pm files are more commonly used than .pl files. (See Chapter 12.)
@INC array contains list of path to standard Perl libraries.
To load an external file, use either require or use.
require("getopts.pl"); # Loads library file at run time use CGI; # Loads CGI.pm module at compile time
To exit a Perl script with the cause of the error, you can use the built-in die function or the exit function.
open(FH, "filename") or die "Couldn't open filename: $!\n"; if ($input !~ /^\d+$/){ print STDERR "Bad input. Integer required.\n"; exit(1); } |
You can also use the Perl pragmas:
use warnings; # Provides warning messages; does not abort program
use diagnostics; # Provides detailed warnings; does not abort program
use strict; # Checks for global variable, unquoted words, etc.; aborts program
use Carp; # Like the die function with more information about program's errors