|

Introduction
Perl is a very useful language to
code in and can be used for many things on the internet. Some
things include, message boards, counters, guest book, mail
processing, anonymous surfing, and database utilities.
Some people ask why learn perl?
If you have a web page you are most likely wanting a form
processor to process your mail, or a nice message board. They’re
quite easy to find free ones on the internet but, if you want to
be able to customize the script at all or install it, you must
have at least a bit of perl background. A lot of people code from
scratch for numerous reasons. A lot of the time you just can’t
find that script that you need free on the web. Another reason is,
if there is a script like that free on the web it would just be
too hard to customize for yourself and it would just be easier to
make one yourself.
All you need to get started on
this tutorial is perl installed on your hosting company. If for
some reason they don’t have perl installed just ask them to
install it. If you don’t know where perl is installed on your
hosting provider ask your admin or if you have telnet access type:
‘which perl’. It will tell you where perl is installed on your
hosting provider.
This tutorial will teach you how
to program basic perl for the web, without further adieu let’s
get started.
Lets get started
Let’s open up good ol’
notepad and put the following lines in it save it as test.pl and
upload it to your cgi-bin.
#!/usr/bin/perl
print
"content-type:text/html\n\n";
$name =
"Your Name!";
print
"Hey, my name is, $name\n";
That program would simply print:
"Hey, my name is, Your
Name!"
Troubles?
- Is perl even installed on
your hosting provider?
- Did you chmod to 755?
- Does /usr/bin/perl exsist?
- Did you upload as ASCII?
This program is nice, but it
doesn’t really help us do anything… Well yes but we need to
learn the basics before we can do more advanced programming.
Helpful trick!
Throughout this tutorial I will
be showing you helpful tricks to save you time and stress while
your programming with your perl. This helpful trick is:
Use print <<end; to print
HTML until the end; terminator has been found.
Ex:
print <<end;
<HTML>
<HEAD>
<TITLE> My perl page </TITLE>
</HEAD>
<BODY>
Hey, this is my perl page
</BODY>
</HTML>
end
;
would be the same as:
print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE> My perl
page </TITLE>\n";
print
"</HEAD>\n";
print "<BODY>\n";
print "Hey, this is my perl
page\n";
print
"</BODY>\n";
print "</HTML\n";
It would be much easier to do it
as it is on the first one.
Okay now that we can write a
simple print statement scripts lets make something half useful.
The following script is just a script which gets data from a form
using variables the form gives out when processed. I will explain
each function that this script goes through.
#!/usr/bin/perl
read(STDIN,
$buffer, $ENV{'CONTENT_LENGTH'});
@pairs =
split(/&/, $buffer);
foreach $pair
(@pairs)
{
($name,
$value) = split(/=/, $pair);
$value =~ tr/+/
/;
$value =~
s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$in{$name} =
$value;
}
print
"content-type:text/html\n\n";
$db_file =
"db_file.db";
open(db_file,">>$db_file");
print db_file
<<end;
$in{‘name’}
$in{‘icq’}
$in{‘comments’}
end
;
close(db_file);
exit;
This program is very simple yet
very useful, first thing’s first, this chunk o’ code:
read(STDIN,
$buffer, $ENV{'CONTENT_LENGTH'});
@pairs =
split(/&/, $buffer);
foreach $pair
(@pairs)
{
($name,
$value) = split(/=/, $pair);
$value =~ tr/+/
/;
$value =~
s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$in{$name} =
$value;
}
This chunk of code can look
intimidating but it’s actually quite simple, and quite necessary
for most perl scripts.
It’s simply put like this, when values are processed through a
server you get something like this for an email variable:
E%20mail=you%40youremail.com That
way you have to actually THINK using this method parses the data
using a C pack, which makes this quite easy to do.
The next bit of code:
print
"content-type:text/html\n\n";
$db_file =
"db_file.db";
open(db_file,">>$db_file");
print db_file
<<end;
$in{‘name’}
$in{‘icq’}
$in{‘comments’}
end
;
close(db_file);
exit;
printing the content-type, which
is required.
Defining the scalar variable, $db_file.
Using the open function we must
first specify a handle for the file, in this case we choose
db_file. The ‘>’’s next means:
> - overwriting
>> - appending
none – reading
using the <<
whatever_you_want_here function we can simple get it to print the
certain variables to the file until the terminator ‘end’ is
found.
Using the close function we close
the file using the handle, and exit; just exit’s the perl script
at that line.
Now that we know the basics about
perl scripts and scalar variables lets go onto arrays.
@array = ("James","Margret","Maggie","John","Peter");
# That string inputs the
following names in a array called just, array.
We are going to play with some
array functions so we can get used to arrays:
Push (@array,Bill); # array has a
value of = ("James","Margret","Maggie","John","Peter","Bill");
|