PERL Tutorial

Perl Tutorial #1

PERL is now a relatively old language but is continually being updated. It was designed principally to produce reports easily and to evaluate the contents of strings. PERL stands for Practical Extraction and Report Language. It can also be used in conjuction with a Web Server to offer Common Gateway Interface (CGI)services for web based ecommerce and database solutions. However, as it is based on the language C it is capable of running as a standalone programming language which is what we will be concentrating on in these tutorials as it is being largely superseded by PHP. Unlike C it is an interpreted language although there is an initial compiler stage where it is checked for errors and it will not run the interpreter until all errors have been rectified. This tutorial will start from the beginning and will assume no previous experience on your part although a knowledge of a programming language would help. On with the show then...

PERL is usually installed by default when Linux is installed although several other packages may have to be installed particularly with regard to database access. It has to be installed on a Windows based PC before it can be used. As I found it difficult to install the Windows version, I'll be concentrating on the Linux (5.8.3) version. The backend database being used is MySQL which is also available on both platforms. As we will not be using HTML, a web server such as Apache or one of the MS offerings will not be required. For the purposes of this tutorial, I shall assume that Linux is being used which in my case is Mandrake 9.x through to 10.x as I couldn't get MySQL to work on SUSE 8.2 which was to say the least frustrating. Mandrake has a very nice installer which makes it easy to check if all the packages required are installed which for the sake of clarity are:

PERL
MySQL

To test they are all installed, open your favourite text editor and create a file called perltest in your home directory or in it's own folder as follows:-

#! /usr/bin/perl

use DBI;
use strict;

my ($dsn)="DBI:mysql:user:localhost";
my ($user_name)="linux";
my ($password)="linux";
my ($dbh);

$dbh=DBI->connect($dsn,$user_name, $password)
	or die "Could not connect";

if($dbh)
{
	print "You are connected to mySQL\n";
}

exit(0);

Don't worry too much about what it all means. You will need to right-click on the file in KDE and set the permissions as Executable. Then in a terminal window type:-

./perltest

If you get an error saying it can't find the file then navigate to the correct folder and try again. If you don't get the following message:-

You are connected to mySQL

then either MySQL isn't installed or it isn't running. To get it running on my system requires the following command to be entered into a terminal session:-

/usr/share/mysql/mysql.server start

and you should get a message saying that it has started. Entering the command:-

mysql

should elicit a prompt:-

mysql>

and you can enter either help or quit to get help or to quit the program. If you get an error then either it is the wrong path or like me you are using SUSE 8.2 and can't get it to work (and no one can tell me why it doesn't work). If all is well then try the command again you should get the message saying you are connected. So much for getting it all running. Now we can get down to some coding. We'll take a look at the program above again a line at a time:-

#! /usr/bin/perl

This instructs the user to look for the perl interpreter in the path specified. This is the standard path for most implementations of Linux.

use DBI;
use strict;

The first line tells the iterpreter that we intend to use databases in the program. The second ensures that all variabkles are declared prior to their use.

my ($dsn)="DBI:mysql:user:localhost";
my ($user_name)="linux";
my ($password)="linux";
my ($dbh);

This sets all the relevent passwords (more on this later).

$dbh=DBI->connect($dsn,$user_name, $password)
	or die "Could not connect";

This is where we connect to the database or stop the program if we can't.


if($dbh)
{
	print "You are connected to mySQL\n";
}

Provided we have a connection then print a message on the screen.


exit(0);

Finish the program.

Don't worry too much on what all this is doing as it will all be covered in future tutorials. I think that just about wraps it up for the moment. If you are having difficulty with any of this then please send me an email and I'll try to help. Look out for the next tutorial coming soon...

Back

Written by Nick Cheesman. Last updated: 01/04/2004
Please eMail me at:
nickjc@nickjc.co.uk