Introductory Perl
Contents |
Introduction |
Perl means Practical Extraction and
Report Language. Those of you who know
awk, sed, and shell scripts will
think Perl is very much like these.
Actually, Perl does much more than each of these. Perl syntax
also looks like these as well as like C. This lab presumes only
that you know C.
There is a good Perl book for beginners by Eileen Quigley, published
by Prentice-Hall called Perl by Example.
Scalar Variables |
The following is excerpted from Jacqueline D. Hamilton's awesome online class at http://www.cgi101.com. (She has now published this in book form and I highly recommend it.) In particular, this material comes from http://www.cgi101.com/class/ch2/text.html.
Perl has three types of variables: scalars, arrays, and hashes.
A scalar variable stores a single (scalar) value. Perl scalar names are prefixed with a dollar sign ($), so for example, $x, $y, $z, $username, and $url are all examples of scalar variable names. Here's how these variables are used:
$foo = 1;
$name = "Fred";
$pi = 3.141592;
$fnord = 23;
$blee = "The magic number is $fnord.";
Let's create a file called first.pl and add some scalars to it:
#!/usr/bin/perl
$classname = "Electronic Documents";
print "Hello there. What is your name?\n";
$you = <STDIN>;
chomp($you);
print "Hello, $you. Welcome to $classname.\n";
Save, and run the script by typing: Perl first.pl
This time, the program will prompt you for your name, and read your name using the following line:
The program will print "Hello there. What is your name?", then pause and wait for you to type something in. (Be sure to hit return when you're through typing your name.) Whatever you typed is stored in the scalar variable $you. Since $you also contains the carriage return itself, we use
prints this new value of $you . As in C, The "\n" at the end if the line is the perl syntax for a carriage return.
Arrays |
Perl array names are prefixed with an at-sign (@). Here is an example:
In Perl, array indices start with 0, so to refer to the first element of the array @colors, you use $colors[0]. Note that when you're referring to a single element of an array, you prefix the name with a $ instead of the @. The $-sign again indicates that it's a single (scalar) value; the @-sign means you're talking about the entire array.
If you wanted to loop through an array, printing out all of the values, you could print each element one at a time:
#!/usr/bin/perl
# this is a comment
# any line that starts with a "#" is a comment.
@colors = ("red","green","blue");
print "$colors[0]\n";
print "$colors[1]\n";
print "$colors[2]\n";
#!/usr/bin/perl
# this is a comment
# any line that starts with a "#" is a comment.
@colors = ("red","green","blue");
foreach $i (@colors) {
print "$i\n";
}
Array Functions |
Since an array is an ordered list of elements, there are a number of functions you can use to get data out of (or put data into) the list:
@colors = ("red","green","blue","cyan","magenta","black","yellow");
$elt = pop(@colors); # returns "yellow", the last value of the array.
$elt = shift(@colors); # returns "red", the first value of the array.
You can also add data to an array:
@colors = ("green", "blue", "cyan", "magenta", "black");
push(@colors,"orange"); # adds "orange" to the end of the @colors array
@morecolors = ("purple","teal","azure");
push(@colors,@morecolors); # appends the values in @morecolors to the end of @colors
Here are a few other useful functions for array manipulation:
@colors = ("green", "blue", "cyan", "magenta", "black");
sort(@colors); # sorts the values of @colors alphabetically
Hashes |
A hash is a special kind of array - an associative array, or paired group of elements. Perl hash names are prefixed with a percent sign (%), and consist of pairs of elements - a key and a data value. Here's how to define a hash:
Hash Name key value %pages = ( "fred", "http://www.wpi.edu/~fred/", "beth", "http://www.wpi.edu/~beth/", "john", "http://www.wpi.edu/~john/" );
This hash consists of a person's name for the key, and their URL as the data element. You refer to the individual elements of the hash with a $ sign (just like you did with arrays):
If you want to print out all the values in a hash, you'll need a foreach loop:
Hash Functions |
Here is a quick overview of the Perl functions you can use when working with hashes.
delete $hash{$key} # deletes the specified key/value pair, # and returns the deleted value exists $hash{$key} # returns true if the specified key exists # in the hash. keys %hash # returns a list of keys for that hash values %hash # returns a list of values for that hash scalar %hash # returns true if the hash has elements # defined (e.g. it's not an empty hash)