PerlPad v3.5 Tutorial

Index 
Introduction 
Part One - What is Perl 
Part Two - A Perl Program 
Part Three - What are these Functions? 
Part Four - Using qq 
Part Five - Using Scalar Variables 
Part Six - Subroutines 
Part Seven - 'if' Statments 
Part Eight - Error  
Part Ten - Debugging 
Note: This Tutorial Is not Complete and more sections will continually be added 
Introduction
In this Tutorial you will learn the basics of perl programming. In no means is this meant to or going to make you a perl programmer or expert but will help you on your way. It will teach you basic skill needed by all perl programmers. This tutorial assumes that you know the basics of html. Also, If you like this tutorial I encourage you to email me at tutorial@paulg.hypermart.net  and give me feedback. And please Rate this at The CGI-Resources.
Part One - What is Perl?
Perl ( Practical Extraction and Report Language) was first released by Larry Wall. It was made for writing programs in a UNIX environment.
Perl has the power of programming languages such as C or C++ but without hassles that come along with those languages. Like shell scripts it does not require a special compiler.So perl is one of the most functional and easy to use programming languages around.

Part Two - A Perl Program
This program shows the most basic of perl's functions. The print function. Look at the sample program below and each part will be explained later.
#!usr/local/bin/perl 
print "Content-type: text/html\n\n"; 
print "Hello Universe"; 
Now we will examine each line:
Line 1:
#!usr/local/bin/perl
This is the location of the perl interpreter on your server. It is usually at: #!usr/local/bin/perl,or #!usr/bin/perl. If you are in doubt and cannot find where it is located anywhere in you server documentation, contact or server administrator. For Example purposes I will use #!usr/local/bin/perl the location of perl 5 on my server.
Line 2:
print "Content-type: text/html\n\n";
This tells the web browser what type of content it will be getting. This is the also the first time you will encounter the print function. This will be explained later.
Line 3:
print "Hello Universe";
This tells the browser what to print. In this case the text Hello Universe.
If you ran this program your browser would read:
Hello Universe

Part Three - What are these functions?
Now you will learn the basis of perl programming. The simple print function. There are many different ways you can use this function. You can include virtually anything with the print function. Anything you can do with HTML you can do here. But there are a few limitations. Take this line of code
print "<a href="http://paulg.hypermart.net">Click Here</a>";
This would not work in your program and you would receive an error. The reason is the Double Quotes (", They are in Bold type). Double quotes are special perl characters. For that reason you have to tell perl not to interpret these as pert of the program but as part of what to print. To do this you put a backslash in front of the double quotes. EXAMPLE:
print "<a href=\"http://paulg.hypermart.net\">Click Here</a>";
Or
print "paulg\@paulg.hypermart.net";
The @ sign is also a special perl character. Many other special perl characters exist and are discussed in other references. If one of those backslashes were missing your whole program would break down.
Some Special Charactors in Perl		
#	Any text placed after this charactor is commented out.	
" or '	Used at the begining and end of many functions	
\	Used so perl doesn't interpret the next charactor as a special perl charracter (Explained Above)	
@	List array	
%	Asociative array	
$	Scalar Variable	
&	Used to access Subroutines	
Again these are just the common perl charactors there are others but they are not used as much as these.

Part Four - Using qq
When you get sick of typing lots of backslashes in your program you will need away to get around it. This is where qq comes in. What this does is it tells perl to use a different character to delaminate The print function.
For example, Without using qq to make the script print, "Hello", she said. You would have to type:
print "\"Hello\", she said.";
But with qq marks you can make another character such as ! be the diliminator. The same statement can be written as:
print qq!"Hello", she said.!;
This increases the readability of you script by you or another person down the road. And helps with debugging. What qq is actually doing isusing the ! sing as the deliminator instead of the normal ", But you can use any charactor you want. EXAMPLE:
print qq@"Hello", she said@;
Part Five - Using Scalar Variables
This is where we really start to get into the programming functions of Perl. Scalar variables are the heart and soul of perl. They make you life easier as well as providing needed functionality.
So, What is a scalar variable? I am sure if you have ever even attempted to look at a script you have seen them. The are denoted by the $. Here is an Example.
$name = 'Paul Gebheim';
How can this be used? Well say that you use your name multiple times in one script. Instead of having to type, in this case, Paul Gebheim, you can just put $name in it's place. Example.
print "Hello My name is $name . I am her to teach you perl.";
In you browser it would appear:
Hello My name is Paul Gebheim. I am here to teach you perl.
This is also very useful with numbers:
$name = 'Paul Gebheim'; 
$age = 14; 
$text = "$name is $age years old"; 
print "<H4>$text</H4>"; 
This would print:
Paul Gebheim is 14 years old
Note: Notice that the line $text = "$name is $age years old"; is in double quotes. This is because it is interpolated. No interpolation occurs in single quotes. Example. If you put the same thing in single quotes( $text = '$name is $age years old';) it would print:
$name is $age years old

Part Six - Subroutines
Now so far all of this is well and good. But what if you have the same piece of code appearing over and over throughout your program ( Maybe the Header and Footer of an HTML page). It is not very fun to have to right: 
print "<html>"; etc.
over and over again. So instead we use what is called a subroutine. A sub routine is a block of data that can be re-used. First I will give you a sample program that uses a few sub routines and explain it:
#! /usr/local/bin/perl 
################################## 
# Script that uses Subroutines # 
################################## 
$name = 'Paul Gebheim' 
$age = 14; 
$string = "$ENV{'QUERY_STRING'}"; 
print "Content-type: text/html\n\n"; 
if ($string eq "name") {&display_name;} 
elsif ($string eq "age") {&display_age;} 
else {&error;} 
sub display_name { 
print "$name"; 
} 
sub display_age { 
print "$age"; 
} 
sub error { 
print " You did not tell me what to do"; 
} 
Ok, line 1 through line 9 should look familiar to you. Except for line 7 where we use the string 
$ENV{'QUERY_STRING'} 
This is how you access all the data in the URL after a ? sign here if you put at ?name after the script name it will store name in the $string scalar or if you put ?age after the url it will store age in the $string scalar. This is complicated and will be discussed more when we talk about the GET and POST methods .But Starting at line 10 we introduce some new techniques. On line 10 through 12 an if statement is used.An if statement test to see if something is true and if it isn't it does something. and if it is it does something. That will be discussed in the next section. 

Part Seven - 'if' Statements
If statements are the "root" of perl programming. They are used to test something, It returns different data depending on what happens. An Example of this is when you log into a system, If your password is correct it lets you in, If it is not it tells you that your password was incorrect. Here is a basic PERL program to demonstrate something like this. This will also use $ENV{'QUERY_STRING'} as in the last example.
#!/usr/local/bin/perl 
############################## 
# Basic program using if Statements # 
############################## 
$Name = "Paul"; 
$InputName = "$ENV{'QUERY_STRING'}"; 
print "Content-type: text/html\n\n"; 
if ($Name eq $InputName) { 
print "Your name and my name are the Same!\n"; 
print "Paul IS the cooloest name, don't you think?"; 
} 
else { print "Your name is not the same as mine.";} 
The Statements that should be new to you are in bold. If you look at lines 8-10, it says: if ($Name eq $InputName) { etc.etc. }, This is the basic syntax for an if statment: 
if (Something) {Do Something} 
else {Do Something Else} 
In this case It checks to see if The Value in $Name and the value in $InputName. To do this it uses the equality operator 'eq'. For more of these Go to the Equality Operators Section. If the value in the two variables is the same it will print:
Your name and my name are the Same! Paul IS the coolest name, don't you think?
If it is not the same it will print:
Your Name is not the same as mine.

Thanks to:
http://paulg.hypermart.net/
mailto:paulg@nettaxi.com

for Providing us with this Great Tutorial

Viotix Incoporated
http://www.viotix.com.tj/
info@viotix.com.tj