![]() |
|
PERL Tutorial #4 So far we have been dealing with functions that are already built into PHP such as date() but you can write your own. Take a look at this:- #! /usr/bin/perl
use strict;
my $total = 0;
my $a = 0;
my $b = 0;
my $sum=0;
sub addthem
{
$total=$a+$b;
return $total;
}
$a=3;
$b=6;
$sum=addthem($a,$b);
print "The sum of $a and $b is: $str\n";
exit(0);
This is a very minor illustration but subroutines are very powerful tools for the programmer and saves writing the same code over and over again. It will produce an output like this:- The sum of 3 and 6 is: 9 So how is it doing it? The variables (called global variables because they apply to the whole program) are defined at the top precede by a $ sign. Then we start the subroutine called addthem preceded by the keyword sub. A block of code is then opened using the opening { brace much like in loops. The variable $total is then given the contents of $a and $b and adding them together (we'll come to where that is being passed in a moment). We then return the sum of the two variables back to the calling program. So where is that? The program calling the sub is further down. You can think of a sub as being a black box - you pass something in and it passes a result back again. To call it, we give the variables already setup above a value each. Those values are passed to the sub by putting the variable names in brackets seperated by a comma. The variable $sum is recieving the value passed to it by the return keyword in the sub. This value is then printed and the program ends in the usual way. This program works fine but the use of global variables would not work well in a very large program. It would be better if values could be passed directly to the sub and keep the variables local to it. This is achieved like this:- #! /usr/bin/perl
use strict;
my $sum=0;
my $total=0;
my $a=0;
my $b=0;
sub addthem
{
local($a);
$a=0;
local($b);
$b=0;
$total=$_[0]+$_[1];
return $total;
}
my $a=3;
my $b=6;
$sum=addthem($a,$b);
print "The sum of $a and $b is: $sum\n";
exit(0);
This gives exactly the same output as before but it is achieved in a slightly different way. This time, we still have the same global variables and they are assigned values in the same way. However, the sub does not have access to them. Instead it has it's own versions set locally within the sub and even though they are the same name as the globals, they won't necessarily hold the same values. The varibales have to be defined as local within the sub and it gets the values to work on by using parameters $_[0] and $_[1] which will contain the values passed to them from the calling code where $_[0] will contain the value held in $a and the $_[1] will contain the second parameter value held in b$. The local variables a$ and b$ will always contain zero. I won't be covering the subject of files in any of these tutorials just yet as I want to move on to using databases but it is sometimes useful to be able to list all the files in a given directory. This is done like this:- #! /usr/bin/perl
use strict;
my $i = 0;
my $name=" ";
my $dirname = "/etc";
opendir("ETC",$dirname) or die("Unable to open");
foreach $name (sort readdir(ETC))
{
print "$name\n";
}
closedir(ETC);
exit(0);
Very simply, it creates some variables at the top including the path to search which in this case is /etc (Windows users may like to use c:\) then the directory is opened. If it doesn't exist then a message is displayed and the program is shut down. Otherwise, each filename is displayed and sorted alphabetically. The directory is then closed and the program exits. Thats it for this tutorial. Next time we'll start using databases. I can't wait... Please eMail me at: nickjc@nickjc.co.uk |
|