<name title="Perl/CGI for newbies" author="woody">
<font ns>
<font name verdana height 22 color 1>
<center>Perl/CGI for newbies #1 by wOODY^dRN</center>
</font><br>
<font name verdana height 12 color 0>
Okay I'm gonna try to explain the basics of Perl in this tutorial.<br>
<br>

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!" :)<br>
<br>

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 ...<br>
<br>

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.<br>
<br>

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

<pre><font name terminal height 12 color 3>
#!/usr/bin/Perl<br>
</pre><font name verdana height 12 color 0><br>

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.<br>
<br>

Next line is:<br>
<br>

<pre><font name terminal height 12 color 3>
  print "Hello world!\n";
</pre><font name verdana height 12 color 0><br>

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.<br>
<br>

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

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.<br>
<br>

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

<pre><font name terminal height 12 color 3>
   \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
</pre><font name verdana height 12 color 0><br>

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 ;<br>
<br>

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

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

<pre><font name terminal height 12 color 3>
#!/usr/bin/Perl

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

print "12 + 12 is: ";
print 12+12;
</pre><font name verdana height 12 color 0><br>

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

<pre><font name terminal height 12 color 3>
#!/usr/bin/Perl

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

print "12h + 12h is: ";
print x12+x12;
</pre><font name verdana height 12 color 0><br>

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

<pre><font name terminal height 12 color 3>
#!/usr/bin/Perl

$number1 = 12;
$number2 = 45;

print $number1," + ",$number2," is: ";
print $number1+$number2;
</pre><font name verdana height 12 color 0><br>

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

<pre><font name terminal height 12 color 3>
#!/usr/bin/Perl

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

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

</pre><font name verdana height 12 color 0><br>
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:<br>
<br>

<pre><font name terminal height 12 color 3>
@myarray = ('1','2','3','4','5');
</pre><font name verdana height 12 color 0><br>

This could be done easier:<br>
<br>

<pre><font name terminal height 12 color 3>
@myarray = (1..5);
</pre><font name verdana height 12 color 0><br>

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

<pre><font name terminal height 12 color 3>
@myarray = (1..5);

print $myarray[1];
</pre><font name verdana height 12 color 0><br>

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

<pre><font name terminal height 12 color 3>
@myarray = (1..5);
$NumberInArray = 1;

print $myarray[$NumberInArray];
</pre><font name verdana height 12 color 0><br>

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:<br>
<br>

<pre><font name terminal height 12 color 3>
@myarray = (1..5);

$NumberOfElements=@myarray;
</pre><font name verdana height 12 color 0><br>

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:<br>
<br>

<pre><font name terminal height 12 color 3>
@myarray = ('woody',23);

($name,$age) = @myarray;

print "The clients name is: ",$name;
print "The age is: ",$age;
</pre><font name verdana height 12 color 0><br>

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.<br>
<br>

<pre><font name terminal height 12 color 3>
@myarray = ('StartUpPic=\pic\start.gif','NewsServer=news.domain.com');

foreach $i @myarray
{
 ($ConfName,$Value) = split(/=/,$i);
};
</pre><font name verdana height 12 color 0><br>

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:<br>

<pre><font name terminal height 12 color 3>
for i:=1 to sizeof(myarray) do
</pre><font name verdana height 12 color 0><br>

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.<br>
<br>

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.:<br>
<br>

<pre><font name terminal height 12 color 3>
@myarray = ('StartUpPic=\pic\start.gif','NewsServer=news.domain.com');

foreach $i @myarray
{
 ($ConfName,$Value) = split(/=/,$i);
 $NewVar{$ConfName} = $Value;
};
</pre><font name verdana height 12 color 0><br>

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

<pre><font name terminal height 12 color 3>
print $NewVar{'StartUpPic'};
</pre><font name verdana height 12 color 0><br>

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:<br>
<br>

<pre><font name terminal height 12 color 3>
  < 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 >
</pre><font name verdana height 12 color 0><br>

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=<whatever name they typed><br>
<br>

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.<br>
<br>

<pre><font name terminal height 12 color 3>
  (*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;
    };
  };
</pre><font name verdana height 12 color 0><br>

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")<br>
<br>

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 ?<br>
<br>

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

<pre><font name terminal height 12 color 3>
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";
</pre><font name verdana height 12 color 0><br>

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

wOODY^dRN<br>




