PERL
Pattern Extraction and Reporting Language
Example:
•
Web page serving through CGI: Perl is used extensively in serving up
content when run in concert with a web-server.
•
Talking to web sites and reporting on bits of info: Perl can go and visit a
website for you, without you ever using a browser.
•
Database Management: Perl comes with the capability to interact with
online "backend" databases -- the massive storehouses of information
running behind the scenes on large websites.
•
System Administration: Scripts for automating almost anything.
Running Perl Scripts:
Create a file hello.pl:
print "hello world\n";
Run it by typing: perl hello.pl
Variables and Types:
Scalars. A scalar is a single piece of information. Scalars can hold numbers or text.
Scalars are given names (just like the unknown x is in algebra) but these names
always must start with a $ sign (for scalar).
$x = 10
$value = $x + 1
$word = "hello"
Arrays. Arrays hold multiple pieces of information that can all be referrred to at
once. Arrays can hold numbers or text and their names always start with a @ sign
(for array). They can have as many values within them as you'd like. Here are some
examples of arrays:
@array = (1, 2 )
@words = ( "first", "second", "third" )
@values = ( $x, $y, 3, 5)
Subscripts start at 0, not 1. Notice that since you are extracting a single value out of
it, you are referring to a scalar, and therefore you change the prefix to a $. Given
the examples above:
$array[0] has the value 1
$array[1] has the value 2
$words[0] has the value "first"
"list context", i.e., put into parentheses. So
($x, $y, $z ) = ( 1, 2, 3);
would assign the values 1, 2, 3 to $x, $y, $z respectively. This "list context" idea
comes up frequently in Perl.
Hashes. Hashes a really great part of perl, and they are extremely useful in
practice. Hashes are just special arrays. They are special because instead of having
numerical indexes into the elements, like [0], they have words as the indexes. The
curly braces suggest that they are fancy arrays.
To make a hash element, you just define it, using a key and value pair:
$servings{pizza} = 30;
$servings{coke} = 40;
$servings{spumoni} = 12;
The keys above are "pizza", "coke", and "spumoni", and the values are 30, 40, and
12. You could use strings for values too:
$occupation{Jeff} = "manager";
$occupation{Martha} = "interior designer";
Here the keys are Jeff and Martha, and the values are manager and interior
designer. If you want to refer to the hash itself, you use a % sign, so these hashes
would be %servings and %occupation.
The default variable. Perl provides a default variable designated with an
underscore symbol: $_. Just as this name suggests, it is a scalar variable with the
name "underscore". This variable is used whenever a variable is required, but where
you're too lazy to bother specifying one.
As a specific example, suppose this default variable, $_, has the value of "hello
world". Since the print statement normally requires a variable to print, if you leave it
out the default variable will be used. So if you write:
print;
then you'll get
hello world
Flow Control
If-Else
The if-else combination is one of the most important control statements in any
programming language. The idea is that if a condition is true, do one thing, and if it's
not true, do something else. An example will show how it works:
$myname = "Mike";
if ( $myname eq "Mike" ) {
print "Hi Mike\n";
} else {
print "You're not Mike!\n";
}
While
$x = 0;
while ( $x < 4 ) {
print "X is less than 4\n";
$x = $x + 1;
}
print "X is finally equal to 4";
For
for ( 1 .. 3 ) {
print "I'm doing this 3 times!\n";
}
Another use for the for loop is working through an array:
@colors = ( "red", "green", "blue");
for $color ( @colors ) {
print $color;
print "\n";
}
Hashes are often used in conjunction with the for loop and the special function
"keys" which returns all the keys from a hash as an array:
$servings{pizza} = 30;
$servings{coke} = 40;
$servings{spumoni} = 12;
for $key ( keys ( %servings ) ) {
print "We served $servings{$key} helpings of $key\n";
}
Some Simple Perl Scripts
To get an idea of how Perl works, we'll finish off the first lesson with some simple
Perl scripts. We'll build on the items you've learned earlier: scalars and arrays, the ifelse, while and for constructs, and the print statement.
Statements
Statements are complete thoughts in perl, just like sentences. In English, sentences
end with a period. In Perl, statements must end with a semi-colon. This is for
good reason — in perl the period is used for something else.
Comments
In perl, comments are set off with the "#" character. Anything following the # to the
end of the line is a comment. For example:
#This illustrates a comment.
There is another form of comment which is very useful when you want to chop out large
sections of code. This technique is used for Perl's embedded documentation, i.e. to create
the POD files:
=comment until cut
$variable = 1;
print "The variable has the value of $variable\n";
...
...
=cut
The Newline Character
print " Skip to the new line after this sentence.\n";
Short Forms
In perl, some operations are so common they have a shortened form that saves
time. These may be strange to the novice, so I'll be careful here.
Programming Errors
If you have a mistake in your perl script that makes your meaning unclear, you'll get
a message from the perl compiler when you try to run it. To check for these errors
before running, you can run perl with the -c flag. And turn on the warnings flag, -w,
while you're at it. This picks up hard to find errors too. As an example you'd type in:
perl -wc hello.pl
to check on the health of the perl script, hello.pl. If there are any syntax errors,
you'll hear about them alright!
Running the example scripts
You can copy any of the following script examples into a file in Notepad and save it
as, say, perltest.pl. Then check it for errors by typing
perl -wc perltest.pl
If it comes back saying "syntax ok", then go ahead and run it by typing
perl perltest.pl
If it doesn't say "syntax ok", then go and fix the reported error, and try again.
Script 1: Adding the numbers 1 to 100, Version 1
$top_number = 100;
$x = 1;
$total = 0;
while ( $x <= $top_number ) {
$total = $total + $x;
# short form: $total += $x;
$x += 1;
# do you follow this short form?
}
print "The total from 1 to $top_number is $total\n";
Script 2: Adding the numbers 1 to 100. Version 2
This script uses a form of the for loop to go through the integers 1 through 100:
$total = 0;
#the for loop gives $x the value of all the
#numbers from 1 to 100;
for $x ( 1 .. 100 ) {
$total += $x; # again, the short form
}
print "The total from 1 to 100 is $total\n";
Script 3: Printing a menu
This script uses an array to store flavors. It also uses a terrific form of the for loop to
go through them.
@flavors = ( "vanilla", "chocolate", "strawberry" );
for $flavor ( @flavors ) {
print "We have $flavor milkshakes\n";
}
print "They are 2.95 each\n";
print "Please email your order for home delivery\n";
Script 4: Going one way or the other:
This allows you to program in a word to make a decision. The "ne" in the if
statement stands for "not equal" and is used to compare text. The "die" statement
shows you a way to get out of a program when you're in trouble.
#You can program answer to be heads or tails
$answer = "heads";
if ( $answer ne "heads" and $answer ne "tails" ) {
die "Answer has a bad value: $answer!";
}
print "Answer is programmed to be $answer.\n";
if ( $answer eq "heads" ) {
print "HEADS! you WON!\n";
} else {
print "TAILS?! you lost. Try your coding again!\n";
}
Script 5: Going one way or the other, interactively:
This allows you to type in a word to make a decision. A shameless sneak peek at the
next lesson on input and output. <STDIN> allows us to read a word from the
keyboard, and "chomp" is a function to remove the newline that's attached to our
answer after hitting the carriage return.
print "Please type in either heads or tails: ";
#The <STDIN> is the way to read keyboard input
$answer = <STDIN>;
chomp $answer;
while ( $answer ne "heads" and $answer ne "tails" ) {
print "I asked you to type heads or tails. Please do so: ";
$answer = <STDIN>;
chomp $answer;
}
print "Thanks. You chose $answer.\n";
print "Hit enter key to continue: ";
#This line is here to pause the script until you hit the carriage return
#but the input is never used for anything.
$_ = <STDIN>;
if ( $answer eq "heads" ) {
print "HEADS! you WON!\n";
} else {
print "TAILS?! you lost. Try again!\n";
}
Introduction to CGI
The rest of this introductory Perl course will describe how you can make Perl interact
with a browser through the Common Gateway Interface, or CGI.
CGI is a standard interface that sits between the web browser and the web server.
When the browser makes a request of the server, all of the request details flow into
the server through the input interface of the CGI. When the server responds with its
output, the information flows back out through the output interface of the CGI.
When Perl responds to a browser request it sends output to STDOUT which is sent
through CGI back to the browser. Because you know how to print data to STDOUT,
you can already work with CGI at its most basic level: sending data to it. It is no
more complicated than printing to STDOUT.
Sending your first page to the browser
Let's look at a simple page:
print "Content-Type: text/html\n\n";
print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>Hello World</TITLE>\n";
print "</HEAD>\n";
print "<BODY>\n";
print "<H4>Hello World</H4>\n";
print "<P>\n";
print "Your IP Address is $ENV{REMOTE_ADDR}.\n";
print "<P>";
print "<H5>Have a nice day</H5>\n";
print "</BODY>\n";
print "</HTML>\n";
First of all, it should be clear that this is Perl program does nothing but print HTML
text to STDOUT. That's pretty much all there is to sending data out through CGI!
The output does have to follow certain rules through. You can't just print anything
you want and expect it to be interpreted by the browser correctly. The first thing that
your program must do is to tell the browser what kind of information follows.
Since we are sending HTML output, we must inform the browser that the content is
text in HTML format. That's what the first line is for. (It's a standard MIME header.)
If you were just outputting plain text to your browser, then you could put
"text/plain" instead of "text/html".
It is very important to send two newlines after the header, i.e., a single blank line,
otherwise the browser will complain of bad header information.
After the content type header, there is just a complete section of HTML. The only odd
bit in the script is the line:
print "Your IP Address is $ENV{REMOTE_ADDR}.\n";
As you have seen, this prints your numeric IP address in the browser window. But
what is $ENV{REMOTE_ADDR}? The answer will become clear later. For now just
accept that it is part of the data delivered to the input of CGI. How you get at data
will come later.
Let's look at a couple of alternatives to repeated print statements because
TIMTOWTDI! Let's quote the whole thing within a variable and just print once:
$html = "Content-Type: text/html
<HTML>
<HEAD>
<TITLE>Hello World</TITLE>
</HEAD>
<BODY>
<H4>Hello World</H4>
<P>
Your IP Address is $ENV{REMOTE_ADDR}
<P>
<H5>Have a nice day</H5>
</BODY>
</HTML>";
print $html;
This is easier because of two things. We don't have to keep track of quotes around
many strings, and we can let the new lines within the block itself specify the \n
characters. But now that we're quoting, suppose we wanted to put the word nice into
quotation marks? If we wrote:
<H5>Have a "nice" day</H5>
in the original script, we'd get a syntax error, because the two quote marks we put in
would match with the opening and closing quotes already there. The solution is to
backquote the new ones, like this:
<H5>Have a \"nice\" day</H5>
This takes away the syntax error since the quotes are interpreted as part of the
string itself. Sometimes though, this backquoting gets to be too much of a pain when
there a lots of quotes such as in the next example. In that case, you can use a
different quoting delimiter, and use the explicit quoting operator, qq{}.
$html = qq{Content-Type: text/html
<HTML>
<HEAD>
<TITLE>Hello World</TITLE>
</HEAD>
<BODY>
<H4>Hello World</H4>
<P>
Your IP Address is $ENV{REMOTE_ADDR}
<P>
<H5>Have a "nice" day</H5>
<p>Name:<input type="text" size="20" name="txt_name" value="name">
</BODY>
</HTML>};
print $html;
Alternatively, you can also use the "here document" form of printing. "Here
documents" originate in Unix shell programming. With qq's ability to quote over
newlines, it’s not clear what advantage they offer. In any case, here's an example:
print <<EOF;
Content-Type: text/html
<HTML>
<HEAD>
<TITLE>Hello World</TITLE>
</HEAD>
<BODY>
<H4>Hello World</H4>
<P>
Your IP Address is $ENV{REMOTE_ADDR}
<P>
<H5>Have a "nice" day</H5>
<p>Name:<input type="text" size="20" name="txt_name" value="name">
</BODY>
</HTML>
EOF
The "here document" always follows the form:
print <<TAG;
...
TAG
It is an alternative to the quotation operator.
These previous examples are very simple since they just print out an explicit string
of html. But Perl begins to be useful in the role of serving content when you make up
the output with your program, like this example of code to print a table of squares.
In this example, we are defining two control parameters, $title and $rows, at the top
in a couple of global variables, and we use two functions to provide the head and
body content of the HTML.
(Caution: global variables are not recommended once you are no longer a beginner.
There's too much potential for inadvertently changing their values, so don't do it all
the time. You've been warned.)
$title = "Table of Squares";
$rows=10;
print "Content-Type: text/html\n\n";
print header();
print body();
sub header() {
return qq{<HTML>\n<HEAD>\n<title>$title</title></head>};
}
sub body() {
$body = qq{
<BODY>
<div align="center">
<H4>$title</H4>
<P>
<table border="1">
};
for $i ( 1 .. $rows ) {
$body .= qq{<tr><td>Row $i</td>};
$body .= qq{<td width="50" align="center">};
$body .= $i*$i;
$body .= qq{</td></tr>\n};
}
$body .= qq{
</table>
</div>
</BODY>
</HTML>};
return $body;
}
Serving Edited Files
Now that you know how to fetch and serve an existing HTML file, you can use it as
the basis for editing. Try saving this web page as "editing.html" in your cgi-bin
directory and running this perl script:
print "Content-Type: text/html\n\n";
open HTML, "editing.html" or die $!;
while( <HTML> ) {
s{<title>.*?</title>}{<title>Sub'n'Serve Demo</title>};
s{<H2.*?</H2>}{<H2>Substituted Header!</H2>}i;
print;
}
close HTML;
Introduction to GET and POST
use CGI;
$cgi = new CGI;
print qq{Content-type: text/html
<html><head></head><body>};
#print every key in the environment
foreach $key (sort (keys %ENV)) {
print $key, ' = ', $ENV{$key}, "<br>\n";
}
#print a couple of simple forms: a POST form and a GET form
print qq{<form method="POST" action=”cgi-bin/getpost.pl”>
<input type="submit" value="Post Request">
<input name="postfield"></form>};
print qq{<form method="GET" action=”cgi-bin/getpost.pl” >
<input type="submit" value="Get Request ">
<input name="getfield" ></form>};
print qq{</body></html>};
CGI.pm does everything you need to read user input, create forms, handle cookies,
handle redirection, and more. It is a very useful module indeed and it is a standard
module supplied with your Perl installation. This next script prints out any input
parameters supplied to the script through either GET or POST.
Try saving this script as getpost.pl and running it in your browser window.
use CGI;
$cgi = new CGI;
for $key ( $cgi->param() ) {
$input{$key} = $cgi->param($key);
}
print qq{Content-type: text/html
<html><head></head><body>
};
for $key ( keys %input ) {
print $key, ' = ', $input{$key}, "<br>\n";
}
print qq{</body></html>};
© Copyright 2026 Paperzz