This tutorial will be covering the PERL syntax and should provide you with a very solid foundation of PERL for you to build upon. It is recommended that before you start walking through our tutorial that you have a general understanding of Web Development as well as some background knowledge of HTML and CSS as our tutorial is directed toward Web programming.
We will be incorporating our PERL scripts with HTML, CSS, and PHP so if you are unfamiliar with any of these languages, it may be a good idea to touch base with the tutorials offered here, or elsewhere for that matter to familiarize yourself with the code we offer.
PERL - Practical Extraction and Report Language
Created in 1987 by Larry Wall, the UNIX based language has evolved into a powerful tool for the internet. It was designed as a quick-fix patch program for UNIX based systems. The language is very simplistic, offering optimum flexibility, perfect for short, straightforward scripting.
Since then its popularity has increased due to its flexibility, portability, usefulness, and its varied features. To get started, load a simple text editor program and follow along in our examples.
PERL - Getting Started
First things first, you go over to perl.org and download the latest version of Perl (currently 5.10) or check that your web host has it installed. We suggest you direct any installation questions or perl problems to the Perl Forum (might want to bookmark this).
This tutorial will be web based, working with and creating files over the internet. File management is the bread and butter of the PERL language, and as you will discover, it's absolutely perfect for doing so.
PERL File ExtensionA PERL script can be created inside of any normal simple-text editor program. There are several programs available for every type of platform. There are many programs designed for programmers available for download on the web.
Regardless of the program you choose to use, a PERL file must be saved with a .pl (.PL) file extension in order to be recognized as a functioning PERL script. File names can contain numbers, symbols, and letters but must not contain a space. Use an underscore (_) in places of spaces.
PERL - First ScriptWith PERL installed we are ready to dive into our first script. There are a few elements every PERL script must contain in order to function. Open up your favorite simple text editor, the file extension for PERL scripts is .pl. Save your files with this extension.
The first line of every PERL script is a commented line directed toward the PERL interpreter. This line is generally the same from one instal of PERL to the next, it might look something like this if you were running it on linux:
firstscript-linux.pl:
#!/usr/bin/perl
And on a windows operating system it may resemble:
firstscript-windows.pl:
#!D:\Perl\bin\perl
In this tutorial we will assume you are running on a linux server (the most common web server), but if you are on windows make sure you use the correct path! The comment points to the installation path of Perl, usually /usr/bin/perl. You can locate the directory tree to Perl somewhere in the documentation of your web server, or email your web host and they can specify your Perl installation directory.
Perl - HTTP Headers
Because we are working in a web environment we are sort of jumping ahead of the game. We have to introduce some HTTP headers so that Perl understands we are working with a web browser. To do this we have to run another line of strange code called an HTTP header as you may have guessed. It looks something like this:
firstscript.pl:
#!/usr/bin/perl
print "content-type: text/html \n\n";
Bookmark and Share
PERL - First Script
With PERL installed we are ready to dive into our first script. There are a few elements every PERL script must contain in order to function. Open up your favorite simple text editor, the file extension for PERL scripts is .pl. Save your files with this extension.
Advertise on Tizag.com
The first line of every PERL script is a commented line directed toward the PERL interpreter. This line is generally the same from one instal of PERL to the next, it might look something like this if you were running it on linux:
firstscript-linux.pl:
#!/usr/bin/perl
And on a windows operating system it may resemble:
firstscript-windows.pl:
#!D:\Perl\bin\perl
In this tutorial we will assume you are running on a linux server (the most common web server), but if you are on windows make sure you use the correct path! The comment points to the installation path of Perl, usually /usr/bin/perl. You can locate the directory tree to Perl somewhere in the documentation of your web server, or email your web host and they can specify your Perl installation directory.
Perl - HTTP Headers
Because we are working in a web environment we are sort of jumping ahead of the game. We have to introduce some HTTP headers so that Perl understands we are working with a web browser. To do this we have to run another line of strange code called an HTTP header as you may have guessed. It looks something like this:
firstscript.pl:
#!/usr/bin/perl
print "content-type: text/html \n\n";
At this point our script still has no real functionality, all we have done thus far is locate our PERL interpreter and tell it that we are going to be working with a web browser or in a web environment.
PERL - Hello, Perl! Script
Now that we have located the interpreter and told PERL we are working with the web, we can print text to the browser with the print function.
helloperl.pl:
#!/usr/bin/perl
print "content-type: text/html \n\n";
print "Hello, Perl!";
Display:
Hello, Perl!
You should see "Hello, Perl!" in the top left corner of your browser. If you have a problem, ask about it on the Perl Forum
Perl - Execute Your First Script
Now it is time to upload your firstscript-linux.pl to your web server and execute it. After you upload your file be sure to CHMOD the script file and allow anonymous execution priviledge, generally a setting of 0755 works perfectly.
Your script is working perfectly if you are staring at a blank screen and didn't recieve a 500 or 404 error message.
Perl - Debugging Your Script(s)
If you are using an FTP program to upload your scripts, set the upload type to ASCII or "Text". This setting prevents the mysterious addition of random characters that sometimes happens when copying files across different operating systems. Learning to do this prevents hours of headaches and frustration.
Another great debugging technique is to isolate the code you are currently working on. To do this you can temporarily comment out lines of code to isolate only the section that is returning an error message.
Bookmark and Share
* Go Back
* Continue
Found Something Wrong in this Lesson?
Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!
#!/usr/bin/perl
print "content-type: text/html \n\n";
print "Hello, Perl!";
Display:Hello, Perl!
You should see "Hello, Perl!" in the top left corner of your browser. If you have a problem, ask about it on the Perl Forum
Perl - Execute Your First Script
Now it is time to upload your firstscript-linux.pl to your web server and execute it. After you upload your file be sure to CHMOD the script file and allow anonymous execution priviledge, generally a setting of 0755 works perfectly.
Your script is working perfectly if you are staring at a blank screen and didn't recieve a 500 or 404 error message.
Perl - Debugging Your Script(s)
If you are using an FTP program to upload your scripts, set the upload type to ASCII or "Text". This setting prevents the mysterious addition of random characters that sometimes happens when copying files across different operating systems. Learning to do this prevents hours of headaches and frustration.
Another great debugging technique is to isolate the code you are currently working on. To do this you can temporarily comment out lines of code to isolate only the section that is returning an error message.