Perl/CGI for newbies #1 by wOODY^dRN

Okay I'm gonna try to explain the basics of Perl in this tutorial.

If you know c++ you shouldn't have any problems learning Perl, cause Perl is just like c++ .. well almost. But if you don't know any programming languages, don't worry .. I'll start with the basics known as "hello world!" :)

You can use notepad or another editor to write the Perl language, but I would prefer a program called Perl Builder, just search www.download.com for this one and it should pop right up. But also HomeSite is pretty good at this ...

If you don't have Perl installed you can get it at www.perl.hip.com for windows and www.perl.com/Perl/info/software.html. I assume that you installed it and ready to code.

First create a file called test.pl. now in the first line write:


#!/usr/bin/Perl

This is so the server you're using for your testing knows where Perl is. Ask the webmaster for the directory if you're not sure about this. You don't have a server to test this ? try www.google.com and search for "free hosting Perl" and you'll get allot of places.

Next line is:


  print "Hello world!\n";

This should be pretty obvious what it does, but if not .. the command print .. prints out text! the \n command means that it should print a newline char.

Now to run this script just write "Perl test.pl" in dos command. And Perl should now write Hello world!

If you downloaded Perl Builder you just press F9 and it will show you the text. What's great about Perl builder is that it has it's own debugger, so you can go line by line in the viewer. Pretty nice tool.

Well back to the \n thing .. there are allot of these "commands":


   \a: alarm or bell
   \b: backspace
   \e: escape
   \f: form feed
   \n: newline
   \r: carriage retun
   \t: tab
   \v: vertical tab
   \$: dollar sign
   \@: ampersand
\0nnn: any octal byte
\xnnn: any hexadecimal byte
  \cn: any control character
   \l: change the next character to lowercase
   \u: change the next character to uppercase
   \L: change following characters to lowercase
       until a \E sequence is encountered.
   \U: change following characters to uppercase
       until a \E sequence is encountered.
   \Q: quote meta-characters as littorals.
   \E: terminate the \L, \Q or \U sequence.
   \\: backslash

Now it should be fairly easy to print strings out now. Just use the print command and remember to end every command with a semi-colon ;

The semi-colon is very important to remember, cause it will not work without this. like this: print "Perl is pretty easy :)"; <---

Now lets get some numerics into this tut :) .. lets make it calculate something.


#!/usr/bin/Perl

# This is a comment ... every line with # is a comment.

print "12 + 12 is: ";
print 12+12;

That was pretty hard ;) ... wanna use hex instead just add a x infront:

#!/usr/bin/Perl

# This is a comment ... every line with # is a comment.

print "12h + 12h is: ";
print x12+x12;

It just can't be any more easier can it ? ... variables is easy too, lets plus to vars and print the answer:


#!/usr/bin/Perl

$number1 = 12;
$number2 = 45;

print $number1," + ",$number2," is: ";
print $number1+$number2;

You could add another var that would have the answer in it:


#!/usr/bin/Perl

$number1 = 12;
$number2 = 45;
$number3;

print $number1," + ",$number2," is: ";
$number3=$number1+$number2;
print $number3;


This will print "12 + 45 is: 57" ... okay I think you get it .. so lets move on to arrays. Arrays are defined with a @ sign. We could make an array that would hold the numbers 1,2,3,4 and 5:


@myarray = ('1','2','3','4','5');

This could be done easier:


@myarray = (1..5);

if we say print @myarray; it would print out "12345" but if we just wanted to display the second number in the array:


@myarray = (1..5);

print $myarray[1];

You can even use a ver instead of defined numbers in the array:


@myarray = (1..5);
$NumberInArray = 1;

print $myarray[$NumberInArray];

Now that would make it print the second number in the array, isn't this easy ? remember that the first number or char in the array is [0], in pascal this would give you the number of elements in the array, this is not the case in Perl. The number of elements is done like this:


@myarray = (1..5);

$NumberOfElements=@myarray;

Lets make an example of a database where your clients name and age is defined. And then move the name and age into some vars, then print them out:


@myarray = ('woody',23);

($name,$age) = @myarray;

print "The clients name is: ",$name;
print "The age is: ",$age;

You could also say $name = $myarray[0]; $age = $myarray[1]; but that would just make the source bigger ... Okay in allot of configuration files you'll see something like StartUpPic=\pic\start.gif or something like that. So to separate strings from a certain sign like "=" here, we use the command split.


@myarray = ('StartUpPic=\pic\start.gif','NewsServer=news.domain.com');

foreach $i @myarray
{
 ($ConfName,$Value) = split(/=/,$i);
};

Okay this might seem confusing but it's not .. first we use the command foreach, that command is like in another language .. etc. in pascal:

for i:=1 to sizeof(myarray) do

It takes the $i var and moves the content of $myarray[0] into it (at first), then it takes the two new vars ($ConfName and $Value) and splits the $i vars (which content right now is "StartUpPic=\pic\start.gif") between the "=" sign in the line. And puts them into $ConfName and $Value, so now $ConfName contains "StartUpPic" and $Value contains "\pic\start.gif". It will do this until there isn't anymore in the array.

But wait .. it will keep overwriting the $confname and $value, so lets make a variable that holds all the info. Here we're gonna use a new type of var.:


@myarray = ('StartUpPic=\pic\start.gif','NewsServer=news.domain.com');

foreach $i @myarray
{
 ($ConfName,$Value) = split(/=/,$i);
 $NewVar{$ConfName} = $Value;
};

Now $NewVar will have several texts in it. To get the contents of it you should address it like this:


print $NewVar{'StartUpPic'};

And it would print out "\pic\start.gif". The same with the other one, just print $NewVar{'NewsServer'} ... But anyways you might ask yourself, wtf am I gonna use this shit for?! Nothing really ;) heh so lets get further to the net stuff. Lets say we're gonna make a script that when the user enters his/her name, it will write back "Hello name .. welcome to my site". Okay first we're gonna create a .html file that gets his/her name:


  < form method=post action="hello.pl" >
  name:< br >< br >
  < input type=text name="name" size=10 maxlength=30 >
  < input type=submit name="login" value="login" >
  < /form >

Now this .html would display a edit box where you can write a name in it. And it would call the Perl script hello.pl. It would call the Perl script like this: hello.pl?name=

So what we're gonna do is to make our script read this line, and split it up so we can print out the name.


  (*fval) = @_ if @_ ;

  local ($buf);

  if ($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN,$buf,$ENV{'CONTENT_LENGTH'}); }
  else { $buf=$ENV{'QUERY_STRING'}; };

  if ($buf eq "") { return 0; }
  else
  {
    @myarray = split(/&/,$buf);
    foreach $i @myarray
    {
     ($Crap,$Name) = split(/=/,$i);
     $NewVar{$crap} = $name;
    };
  };

Don't worry about all this ... it just gets the line and splits it up. What you can see is that first it splits up the line between & signs .. because if there were several vars in the line it would look like this: hello.pl?name=woody&age=23&street=fakestreet that's why it does that. It puts all these text between & sign into the array @myarray, so @myarray would hold @myarray("name=woody","age=23,"street=fakestreet")

But in this case it only contains name and woody. So now it splits up the "name=woody" into name and woody, so $crap would hold "name" and $name would hold "woody". So if we should print out the name, we should write print $NewVar{'name'}; easy ?

So the only thing left is to display the name for the user:


print " Content-type: text/html\n\n";
print "< html >\n" ;
print "< head >\n" ;
print "  < title >hello test< /title >\n" ;
print "< /head >\n" ;
print "\n" ;
print "< body >\n" ;
print "Hello ",$NewVar{'name'}," .. welcome to my site\n";
print "< /body >< /html >\n";

That was easy right ? ;) Next time maybe I'll make some file and procedure routines for you. That's all folks :)

wOODY^dRN