Classnotes | UNIX01 | RecentChanges | Preferences This is not a Perl programming class. As such, we really cannot spend too much time examining Perl commands in the detail that is necessary to make a person a Perl guru. We will provide you here with the fundimentals to get you started, but if you have a need or just an interest for more knowledge, then you will have to look elsewhere.
The links provided previously are a great place to start. Additionally, you can find some exellent books on Perl from O'Reilly, SAMS, Osborne and other publishers.
Variables and Operators
As we mentionned before, Perl is a loosely typed language. Thus, you needn't declare a variable's type before it's use. Variables can be created arbitrarily and are defined at creation. All scalar variables are specified by "$". The following are valid variables and declarations:
Perl allows for arrays through the use of "@". Array elements can be dereferenced using "$" and the element number. For example, the following are valid array declarations
$somearray[1] # would be 2 from above
$newarray[2] # would be "Three" from above
Perl also allows for things called hashes, which we will not use in this course. But the idea is similar to arrays, except instead of numeric indexes, there are alphanumeric ones. Hashes are specified with "%".
Operators
Perl allows for the basic mathematical operators that all other languages have:
+, - : Addition and subtraction
*, / : Multiplication and division
You can use "+" with strings or numbers, for example
$a = 1 + 9;
$b = "One" + "Nine";
Perl also includes things like modulo and bitwise operators which we will not cover.
Basic Commands
print
print is the command to output data to stdout or to some other stream (could be a pipe, a file, or even the web server). print has the same control characters as you find in many other languages such as:
\n : newline
\t : tab
\" : double quote
To simply print to stdout, the format is
print "This is some text to be printed\n";
If you wanted to print to a stream that is called "SOMEID" you would use
print SOMEID "This text will be sent to whatever SOMEID specifies\n";
If you want to print out a variable in a string of text, you can do it this way
print "The color of the sky was $color\n";
however, this practice is frowned upon (even though it is very widely used). A "prefered" alternative is to use the "." character to join text together:
print "The color of the sky was " . $color . "\n";
split
If you have a scalar $a, and wish to split that scalar into an array based upon some deliminator, then the command to use would be split. split returns an array.
For example, if I had the scalar
$a = "Joe:Joe Jorgenson:345-6794";
and I wished to split that into an array using the deliminator ":", then my command would be:
Given a variable (scalar or array), chomp will eliminate the newline(s) at the end.
chomp $a;
chomp @array;
Loops and Conditionals
while
The while loop runs a sequence of commands based upon some condition. The basic syntax is
while ( some condition is true ) {
.... commands to run ....
}
Inside the brackets can be any Perl commands you need to have run. Inside of the (..) can be commands or a condition (anything that returns a "true" or "false" response). For example, if I wanted to run a loop while the variable $i is greater than 10, I would have
while ( $i > 10 ) {
....
}
do...while
The do...while loop is very similar to the while loop, except that it runs through it's loop at least once (whether the conditional in the "while" segment is true or not). It's syntax is
do {
....
} while (some conditional) ;
For example, if I wanted to run through my do...while loop while the variable $count is less than 100, I would have
do {
....
} while ( $count < 100 );
if...else
The if...else commands can be used to test conditions and optionally perform commands. The basic syntax is:
if ( some conditional ) {
....
} elseif ( some other conditional ) {
....
} else {
....
}
For example, if I wanted to test to see if the value of $jury is less than 12 or not, I could use
if ( $jury >= 12 ) {
print "The jury is at least 12\n";
} else {
print "The jury is less than 12\n";
}
foreach
If you have an array, you can use the foreach sequence to run through the elements in that array. For example, if I had the array