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
London, United Kingdom | Utilyx
Senior Business Analyst - London Highly professional individual capable of working at senior / board level with blue chip clients - shaping and driving the analysis and design of their energy management solutions Proven capability ... more >
United Kingdom | Sussex HIS via Acertus Ltd
Business Development and Partnership Director - £62,337 to £77,179 plus benefits Any Sussex HIS location by agreement  The Sussex HIS was formed in mid 2004 through the merging of all IT services from all Trusts ... more >
London, United Kingdom | Barts and The London NHS
 Information Manager - £28,924 - £38,591 pa inc - London   Applications are invited for the post of Information Manager in the Head Office of the Central and East London Comprehensive Local Research Network. The ... more >
London, Berkshire, United Kingdom | EDS
EDS are currently looking to recruit an experienced Change Project Manager to support our Programme Directorate Defence team in Reading, Berkshire. Summary: This role sits within a secure site and will be to work on ... more >
More job opportunities