We’re going to take a look at the basics of Perl, which is a popular choice for writing basic CGI scripts – programs that interact with a web server to provide more dynamic web pages.
For many tasks, open-source developers use more modern alternatives, such as PHP – which includes some functions taken from Perl – but it’s still a good way of getting to grips with creating basic CGI stuff.
You’ll find that just about every Linux system has Perl installed on it these days, and you can also download a free Perl for Windows from activestate.com.
First principles
Perl is a fairly straightforward language to pick up; like PHP (and unlike some
other, stricter languages), it’s not ‘strongly typed’, which means you don’t
have to declare variables before you use them, and say what sort of thing they
are. So you can say something like
$a = ‘hello world’ ;
$b = 12 ;
print $a ;
And so on. As you can see, each statement needs to end with a semi-colon, and strings are enclosed in quotation marks.
One of the first things to remember here, as with some other languages, is that the type of quotation mark is important; if you use a single one, the string is treated literally; use double ones, and special characters are expanded, and variables are substituted. So, if the code above was followed by
print ‘$a\n’ ;
what you’d see as the result is just that – ‘$a\n’ – whereas
print “$a\n” ;
would display the message ‘hello world’ followed by a new line. So if a script doesn’t do what you expected, check you’ve used double quotes in the right places.
How do you run a script? Well, if you want to do a simple test, use Perl interactively: open a command window and type perl, followed by Return (on Windows, you may need to change to the folder containing the Perl executable).
Then type in your script, line by line, and finally type a blank line and then Ctrl & D followed by Return. Normally, of course, you’ll want to do something a little more sophisticated, so create your perl script in a text file and save it with the .pl extension. Then you can run it by typing
perl scriptname.pl
On Linux systems, if you include the magic first line
#!/usr/bin/perl
and use chmod to make the script executable, you can run it just by typing its name, such as ./scriptname.pl. Normally a line beginning with a # is a comment, but the first line is special on Linux, and tells the system where to find the script interpreter for the text that follows.
Blocks and loops
Much programming with any script involves working through blocks of information
using loops, and Perl is no exception. In fact, one of the things it was
designed for was handling text files, and in the normal course of events, Perl
expects to read information that’s fed into it, either on standard input (such
as by typing, or as if typed), or from a list of filenames on the command line.
Here’s a very simple example script
#!/usr/bin/perl
# simple test script to read a file and print it
#
while (<>) {
print $_ ;
}
Save the file as test.pl, and then run it using the command
perl test.pl test.pl
The second test.pl could be replaced by the name of any other text file. Looking at the script, you can see that the block of commands in the loop is enclosed by curly brackets.
The while loop is used with <> to read all the information from a file reference, or handle, and the special one <> means the standard input, while the special variable $_ means the current line of input. So the script reads standard input, and prints it out. If you just typed perl test.pl, it would wait for you to type a line, then print it out again, until you type Ctrl & D to signify the end.
You also need to use brackets in if statements – ordinary around the condition, and curly ones around a block. In this section of code, we’ll introduce another thing Perl’s particularly good at – pattern matching.
The =~ operator compares a variable with a pattern to see if there’s a match; the opposite is !~, for no match. Add the extra lines to the script above, so it ends up like this, then run it with the command perl test.pl test.pl:
while(<>) {
if ( $_ =~ /Fred/ ) {
print “Fred is here, line 4
$line: $_\n” ;
}
$line++ ;
}
The results will be two lines of code, each preceded by a message such as ‘Fred is here, line 7.’
All OnlineTags: Web Development
