Choggy's Beginners Guide to Perl



Information for other Freeola customers

Before I continue with the basics of Perl scripting, I would like to make some points for other folks whose sites are hosted on Freeola. If you've already tried to get Perl scripts working on your site and you can't quite work out why they're not working, the following points are relevant to you:

  1. Your Perl cgi script must be placed in a directory within the main htdocs directory and must have a ".cgi" extension to the filename.

      "What's the cgi-shl folder for, then?" I hear you ask. Yeah, I'm baffled by that too!

  2. The "shebang" line should read:

      #!/usr/bin/perl
The remaining points are general for Perl scripting


Lets begin at the beginning...

The following points may seem obvious, but they're things which aren't often pointed out. If they're too dull for you to deal with, then by all means skip down.

(This list is not finished and new points will be added and organised when they occur to me!)


File Access Rights and the CHMOD command

There's three different things you can do with a file on a web server: Also, depending on where you're looking at it from, the web server will think of you as one of three different people:

These abilities and 'people' can be sumarised in Unix something like this:

Confused? No problem, the figure below explains what it means

Unix File Permissions
Unix File Permissions

So, how do you tell the server what access rights to give to a file? Well, that's where that odd word 'CHMOD' comes into it (usually pronounced pretty much how it looks - "Ch" (like at the beginning of "Church") "Mod" (as opposed to "Rocker")). CHMOD is short for "Change Mode" and is a unix program, written specifically to allow the access rights of a file to be changed.

When you use CHMOD, you can tell it how to assign permissions by typing out the permissions in full, as in the example above. However, most people find CHMOD's numerical notation easier to use (and quicker to type). The numerical notation simply assigns values to each group, based on the permissions you wish to assign to the file. Read access is "worth" 4, write is "worth" 2 and execute is "worth" 1, so if you wanted to assign rwx access, that would translate to:

So the above example would translate to


The Shebang Line

Odd name, isn't it? That's Unix for ya.

Despite the odd name, the shebang line is a very simple concept. It tells the shell where to find Perl. Without it, the shell won't know where to find Perl and your script won't run.

Easy, huh?


The Really Basic Stuff

This following are the things that every Perl programmer needs to become extremely well aquainted with. They're commands and concepts that you'll use every time you write a Perl script.

Print

This is the command used to get something onto the screen. The clichéd beginner's program (in any programming language) prints "Hello World!" on the screen and to do that in Perl, you would use the statement

Commenting

Comments are lines in your script which are intended not for the computer, but for humans reading the script. If you plan to offer your script for others to use, comments are technically a courtesy, but an essential one (or at least they are if you don't want to be bugged by people asking what bits of your script are doing). More importantly however, comments are vital if you ever return to a script you've written previously and have to try to remember what it does and how it does it. To make a line in a script into a comment, just put a hash (By which I mean # - I know that in the US, that symbol is referred to as a pound symbol, but I'm British, so to me a pound symbol is a £) in front of it, e.g. N.B. The Shebang line also starts with a hash (#). This doesn't make it a comment, though. The Shebang line isn't actually part of your perl script - it's an instruction to the shell and that's why it needs a hash.

Input from the User

Quite a lot of the point of a program is to let the user specify something that your program will then work with.

It's quite easy to get input from the user, but we have to store it in a variable. For the sake of this example, let's say we have a variable called $Dinner (Don't worry about why there's a dollar sign there for now, that's explained later - if you're really curious, you can click on the link I created for the word "variable" a couple of lines back). We could use:

That <> bit instructs the code to wait for input from the keyboard. It'll take everything you type, until you hit the Return key, and it'll shove it all in the variable we called $Dinner.

Subroutines

It's possible to write a script which just executes one bit at a time, but quite often you'll want to do some things more than once. For this, you'd want to use a Subroutine. Subroutines are included in the text of the code, at some convenient point (usually at the end of the code proper) and look a bit like this:


Variables

A Variable is probably a familiar concept to anyone who's ever done any programming, but for those of you who haven't, try this analogy on for size: Think of a small set of draws - the kind of thing your Dad might have in his shed for keeping washers and bolts and nails and other little bits and pieces in. Each of those draws has a lable on it, so you can tell what's inside it, right? And probably there's draws of different sizes, to accommodate different sized "things". Well, a variable is like one of those little draws. It's just a place to store something, and is labled so that we know what's in it. A variable (draw) can be almost any size you like, to hold one small nail or an entire elephant (though I imagine your Dad probably doesn't have any elephants in the draws in his shed!)

Of course, bearing in mind that the term "variable" is to do with computers, they're not for storing nails or elephants, but data. You could have a variable called $name which might contain the string Joe Bloggs or a variable called $age which might contain the data 31. You could even have a variable called $MyCV which contains all the text of your Curriculum Vitae (or Resume) though this might be slighly unwieldy! (Note to self: Check whether there's a maximum length for strings stored as Perl variables - if so, the above illustration may need to be changed)

In Perl, a variable which is used to store a single item of data is called a "scalar" variable and the name will begin with a dollar sign ($). The next character after the dollar sign must be either a letter or an underscore. Therefore...


These are valid variable names...    but these are not,    because...
$variable1 $1stVariable the characer after the $ is a number
$Widgets Widgets variable names must begin with a $
$_whazzup $-whazzup the characer after the $ is a hyphen
$MyName $ my name spaces are not allowed in variable names
$And_The_Rest $&_The_Rest the characer after the $ is an ampersand

To set up a variable in Perl, all we need to do is assign a value to it (analogy: put something in the draw). The "assignment operator" in Perl is the equals sign (=). So, for example, we might use these statements:

You'll notice that the data we assigned to the variable $name was in double quotes, but the data we assigned to the variable $age was not. This is because a string of text (such as Joe Bloggs) must be contained inside quotes if it is to be assigned to a variable, but numbers mustn't. If we'd assigned "31" to age, rather than 31, we would have defined the variable's contents as text and would find that we wouldn't be able to perform any mathematical operations on the data, because it's not being treated as a number, but as text. Introuducting this concept would make this a good time to explain about...


Arrays (a.k.a. Lists)

Creating a Scalar Variable is all very well if we want to store one person's name. But what if we want to store a list of names? Of course, we could define a whole load of variables, called $Name1, $Name2, $Name3 etc, but this would get confusing and use an awful lot of code to do something very simple.

A more economical and tidy way to achieve this would be to set up a List.

Just as the names of scalar variables always start with a dollar sign ($), the names of Lists always begin with an at symbol (@), for example, we might define a list called @Names.

We can define a list either as empty or with elements in it. To define a list as empty, we can just say:

To define the same list, but put some elements into it, all we need to do is supply the list of what we want to put in our list, inside those brackets (parentheses, to the americans among my audience) quoting them and separating them with commas, like this:

In Perl, there's actually another way to do this same thing. We can use the qw (Quote Word) construct, which does the same thing, but without the brackets (parentheses), quotes and commas. To achieve the same thing as above, but using qw, we would start and terminate the list of items with a forward slash (/) and separate each item in the list with a space, like this:

The disadvantage of using this method, is that there's no way to differentiate between two separate items in the list, and an item made up of two words.

This is all very well if we know the elements we want in our list when we're writing the code, of course, but what if we don't? Well, we can put elements in the list by using scalar variables. Let's assume that we've got a variable called $User, and at some point in our program, we're going to ask the user to enter their name and the program will store it in $User. We could put their name into our @Names list like this:

Or if we wanted to put it at a specific place in that list, we could do this:

See the difference? The second example tells Perl where to put the item, that's what the [4] bit is doing. It's important to point out that Perl counts from 0, so position 4 actually holds the fifth item in the list (the first item in the list is at position 0)

If you wanted to get really funky, you could even initialise a list by using another list. It doesn't matter too much if you don't get that idea right now, but let's see if you can understand this example:

In this case, our @MoreAnimals list would end up looking like this:

Actually, to be pedantic, if we tried to print our @MoreAnimals list to screen by saying

We'd actually end up with this:

None too pretty, I think you'll agree. To tell Perl we want to have the list items separated by something, we can tell it what we want Perl to space the items out with by using the function join(). So if we said:

The list would end up looking like this:

Alternatively, you could use:

To tell Perl to print out each list item on a new line as we did above


Concatenation

Note to the reader - I'm trying to sort the following out in my head at the moment (with regards to what the actual operators are etc). The principles I've outlined here about Concatenation are sound, but the syntax I use may not be, so please don't take the next bit as gospel.

Complicated word, but all it basically means is 'tagging one thing onto the end of another'. In Perl, the "Concatenation Operator" (that is, the symbol you use to tell Perl to string two strings of text together) is a Full Stop (or Period) also known to techie folk as "dot". For example, consider the following example:

Those first two lines should be fairly easy to understand - they set up two variables, one called $FirstName, which contains the string "Joe" and the other called $LastName, which contains the string "Bloggs".

The last line of that little script, however, does several things in quick sucession. It sets up a variable called $FullName, then it puts the data from the variable $FirstName into the newly created variable $FullName, then it tags a space on the end of the current contents of our $FullName variable (hence the double quotes with a space between them), and finally it tags the contents of the variable $LastName onto the end of $FullName. So $FullName ends up containing the string "Joe Bloggs"

Easy when you know how, eh?

So, what do you think happens in this following script:

If you guessed that $WeeklyWage ends up as 390, I'm afraid you're wrong. We're dealing with text strings and concatenation, so we've actually made $WeeklyWage 30090. I don't know about you, but I'd be quite happy with a weekly wage of £30,090!


Control Structures

The Control Structure is one of the basic tenets of Programming, so the chances are that anyone who knows anything about programming will already know what control structures are and how they benefit the programmer. For those of you who aren't already au fait with the concept, however, here's the low down.

One of the most usful things about a computer program is that you can give it instructions like:


...and these allow the programmer to write code which is applicable to a whole host of situations. Theoretically a program should be able to deal with any and every situation which arrises, whether the programmer anticipated the situations or not (this is called making a program
robust and is one of the main things that a programmer strives for after making the code do what it's supposed to do)

In Perl, there are several Control Structures available to the programmer. I'll deal with them one by one, but first it's important for you to know the truth.

The Truth

Of course, in real life, there are rarely single incontravertable truths. Forget this line of thinking! In computer programming, the truth is testable and confirmable and vital if you want to use control structures. "True" and "False" are the only two permissable values of Boolean Variables and for numberical values, they eqate to 1 or 0 (True equalling 1 and False equalling 0). There are three basic ways of telling if something is true in Perl:

There are some oddities here, though. A text string of "0", or even "000" is false, however a text string of "0.00" is true (because there's a dot in there). Another interesting thing to note is that the expression "0.00" + 0 turns out to be false, because the computer adds 0 to 0 (albeit 0.00, in actual fact) and gets 0 - which makes the statement false.

(Don't worry too much about that bit for now - it'll make sense in time, if you think about it.)

Right, that should be all you now need to know about the truth to understand control structures, so without delay, let's get on with it...


Operators

A major part of any programming language is it's operators. They're what you use to tell the computer what to do with all these nice bits of data. In Perl, the common operators are as follows:

Operator Function Symbol(s) used Description Example
Arithmetic Operators
Multiplication * (asterisk or "star") Multiplies numbers 3 * 2 result 6
Addition + Adds numbers 3 + 2 result 5
Subtraction - Subtracts the second number from the first 3 - 2 result 1
Division / (forward slash) Divides the first number by the second 3 / 2 result 1.5
Modulus % (percent) not sure check back soon
Exponentiation ** (two asterisks or "stars" with no space separating them) not sure check back soon
String (i.e. Text) Operators
Concatenation . (dot) Adds one piece of text (e.g. the contents of a variable) onto another $a="Hello,";
$b="World!";
print $a . $b;

results in the text Hello, World! printed to the screen

Multiply (string) x Repeats a specified string a given number of times print "Hello!\n" x 3;

will print Hello! three times (on separate lines)

Assignment Operators
Assignment = read as "gets set to" $Number = 5

puts the value 5 in the variable $Number

Increment {any operator}= Adds the current value of a string (or whatever) to whatever you want to add something to $a="Hello,";
$b="World!";
$a .= $b;

adds the contents of $b onto the end of the contents of $a, so that $a ends up as "Hello, World!" (while $b still contains "World!"

Please note that incrementation works with almost any operator, for example:

    $a *= 3 means multiply the current value of $a by three and store the result in $a

    $counter += 1 means add 1 to the value of $counter and store the result in $counter

    $FirstBit .= $NextBit means add the contents of the variable $NextBit onto the end of the current contents of the variable $FirstBit and store the result in $FirstBit

Increment / Decrement by 1 ++ or -- (respectively) Can be placed before or after a variable and adds or subtracts exactly 1 to or from it $Age++

makes $Age equal to $Age plus 1

This form of increment is special, because placing it before or after the variable effects what value is returned from an expression, so that:

    $cows=5;
    $sheep = ++ $cows;
does the following:

First, it assigns the value 5 to the variable $cows.

Next, it adds 1 to the value of $cows and stores the value in $cows plus it assigns the (now incremented) value of $cows to $sheep, so both $cows and $sheep end up being 6.

However,

    $cows=5;
    $sheep = $cows ++;
would do this:

First, it assigns the value 5 to the variable $cows.

Next, it it assigns the value of $cows to $sheep then it increments $cows, so $sheep was assigned the value of $cows before $cows was incremented, so although $cows ends up being 6, $sheep is 5.

Logical (i.e. Boolean) Operators
Logical AND && (double ampersand) The expression will evaluate to true if both sides are true $a && $b

For a variable to be considered true, it has to have been defined and not be equal to zero (whether it's a string or a number). So I think the following is how this expression would work:

    $a = 4;
    $b = 7
    $a && $b
Would return a value of true (because they've both been defined) however,
    $a = 4;
    $a && $b
Would return a value of false (because only $a has been defined)
Logical OR || (double "pipe" or vertical bar character The expression will evaluate to true if one of the sides is true) Works similarly to the logical AND
Logical NOT ! (Exclaimation mark) The expression will evaluate to true if the variable is false, and false if it's true $a = 4;
!a$

evaluates to false

Comparison Operators
I haven't got this far yet - check back soon