image: perl screenshot
Just enter aa script manually and you can check it with Perl
R E L A T E D   C O N T E N T
ADVERTISEMENT

Getting started with Perl

A look at the basics of the Perl scripting language for web developers

Nigel Whitfield, Personal Computer World 23 Jan 2007
ADVERTISEMENT

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 Online
Tags: Web Development

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S
M A R K E T P L A C E
Get your free demo of Numara Track-It! 8 - the leading help desk solution for IT related issues.
Make presentations, review documents & share your entire desktop. 30-day free trial! (cc required).
Discover how remote support can fuel your IT business in ways you've never thought of before.
Apply ITIL best practices at your service desk while eliminating integration cost. Learn more here.
WAN based, automated, daily vulnerability assessments. Click here to try and request our whitepapers.
Have your product or service listed here >   
Sponsored links
F E A T U R E D   J O B S
Colindale (C1905), United Kingdom | NHS Blood and Transplant
 Operations Engineer, £28,313 - £37,326 pa plus High Cost Area Supplement, Colindale (C1905) About us The National Blood Service is an integral and vital part of the NHS. Our two million volunteer donors contribute 1.6 ... more >
London, Waterloo, United Kingdom | Christian Aid
Senior Web Designer, £37,526 - £42,257 per annum, London, Waterloo The Senior Web Designer is a crucial post in the Publishing Team and provides creative design and graphic resource for all Christian Aid's websites, with ... more >
Chichester, United Kingdom | West Sussex County Council
  Testing Manager, Chichester, £42,222 - £45,090 (includes a Market Supplement, subject to review) The IT Testing function has until recently been performed within the project structure.  This new role recognises that we need to ... more >
United Kingdom | London School of Economics and Political Science
  London School of Economics and Political Science The Library Analyst Programmer (fixed term 24 months) Salary: £30,201 - £36,563 pa incl The Library is at the heart of LSE, one of the world's greatest ... more >
More job opportunities