![]() |
PERL Tutorial #2 Welcome to the second tutorial. Hope you got on alright with the first one. No? Send me an email if there is something you don't understand and I'll try to help. Last time, we created a file from scratch. It might be helpful to create a template file that we can use for creating programs in the future. First a quick recap. A PHP program resides in a file on the server and can send HTML code to the browser to display information that will be rendered as a web page. If you run the previous program that tests MySQL, if you look at the source of the page then you won't see any of the PHP code only the HTML. On then with the template file. It will look something like this:- #! /usr/bin/perl use strict; my $i = 0; # begin code here exit(0); Not much to it yet (we will update it later when using databases). This is very similar to the file we created before. The # hash denotes a comment. The use strict is there to help the perl compiler spot errors but isn't strictly necessary. The my prior to the $i = 0 prepares the variable for use and again is used by the compiler to spot errors. So on to some real coding... In the tradition of all good (?) tutorials, we start with the familiar Hello World program and here it is:- #! /usr/bin/perl print "Hello World \n"; exit(0); This uses the print statement to write to the screen. Note the \n which creates a newline and the program will print something like this:- Hello World in the terminal window. Now to find out a little bit more about the computer we are using. These are held in what are called Environment variables held by the computer operating system. These can be accessed by Perl as follows: #! /usr/bin/perl Note that the ENV variable surrounds the quoted term with curly brackets. These two commands will print the Present Working Directory and the Home directory of the current user. On my machine this produces:- /home/linux/Perl The first thing to get to grips with are variables (they are called variables because their contents can vary). These are place holders for information that can then be used from within other commands. $strPath is a variable. If you are familiar with other programming langugages then you might be tempted to think that $strPath means it is a string variable - it doesn't. Variables in Perl are not strongly typed. This means that $strPath can hold anything from strings (characters) to numbers. You may be familiar with Visual Basic (or Sinclair SuperBasic on the QL) which has a Variant type that can hold anything - well Perl variables will then be a familiar concept to you. However, even though it can store anything (the $ dollar merely denotes that it is a variable) it is good practice to use a variable name (which can be any length) to give an idea of what it is likely to contain. Here, I expect it to contain a path which is a string so I have called it strPath. So we use the special Perl command that gives us the particular environment variable we are interested in and assigns it to the variable $strPath. Note that in Perl single = equals signs mean assignment while double == equals means equality (as in the C programming language). This information now stored in $strPath is then printed to the terminal window and a \n is also included to force a newline. Next we collect the second environment variable which will overwrite what is in $strPath - this doesn't matter as we don't need to use it again but if we did then we would have to use another variable name. This is also sent out to the terminal and the we exit from the prgram as before. And thats how we do variables. By the way, if you want to find out what version of Perl you are using, this can be done from the command line as follows:- perl --version On my system this gives:- This is perl, v5.8.3 built for i386-linux-thread-multi There is also extensive help available using the command: man perl This next bit may need a little more concentration as we are to embark on the tortuous subject of loops. It took me many months to get my head around loops and nested loops still give me a headache. Take a look at this. Here is a typical FOR loop. #! /usr/bin/perl
use strict;
my $i = 0;
for($i = 1; $i <= 10; $i++)
{
print "i = $i \n";
}
exit(0);
Remember that it goes in the template file and needs to be renamed to something else like perlfor.php for example. We'll take it a line at a time. for($i = 1; $i <= 10; $i++) It is split into several parts. The for( is the beginning of the loop command. The variable $i is assigned (or set to) the value of zero. Zero is very important in computer languages and omitting it could lead to the program not behaving as expected as the initial value of $i could be anything - better to be safe. The next bit ($i<10)will allow the loop to operate until the value of $i is just less that 10 (in this case 9) but as we have started the loopm from zero, there will still be ten values printed. Loops generally start at zero and although initially confusing, it becomes second nature after a while. The third part ($i++) will increment (add to) the value of i$ by one (the ++ means increase by one). Each part is seperated by a semi colon and the closing bracket ends the command. Mistyping any of this will generate an error. Next we have his funny thing:- { Anyone familiar with the C programming language will recognize it at once. It tells Perl that a block of code follows. You often find it shown at the end of the preceding line but I prefer it this way. Stricly speaking, the block brackets are not required when only a single line of codes follows but I think it is a good habit to get into and makes the code clearer. print "i = $i \n"; This is the by now familiar print statement. It will print a short message then the value of $i followed by a break(note the semi-colon ; to show the end of the line). This will have the effect of listing this:- i = 0 Now what on earth is happening here? The loop starts at zero. It will then follow the curly brace until it reaches the print line. At that moment, $i is still zero so that is what is printed. The loop will then increment $i (using the $i++ command) which will increase the value of $i to 1 and so it is printed again. This will go on until the value of $i is one less that 10 at which point it will stop. That is a FOR loop. You may need to take a few minutes to make sure you understand what is going on here. This is a fairly mundane use for a loop but they are very useful and crop up all the time saving the programmer time writing it all out in full and is quite an elegant piece of code. I think that will do for this tutorial. Next time we'll get into more loops. Until then... Please eMail me at: nickjc@nickjc.co.uk |