Broad Network


Getting Started with ActivePerl

Basics of ActivePerl - Part 1

Forward: This is the first part of my tutorial series, Basics of ActivePerl.

By: Chrysanthus Date Published: 26 Jul 2012

Introduction

Perl stands for Practical Extraction and Report Language. It is a scripting language, which is very good in manipulating text. Today you can use it to do other things that powerful languages like C++ achieve. When Perl was invented it was not made to run on windows and some other operating systems. ActiveState is a company that came up with a Perl that could run on windows and some other operating systems. ActiveState’s Perl is called ActivePerl. This is a series of tutorials on ActivePerl. Everything in this series is for ActivePerl. Every thing in this series is applicable to traditional Perl (the first invention and its later versions).

The latest version of ActivePerl is version 5.10.1 Build 1006. It works with the following operating systems: AIX 5.1 or later (rs6000), Linux: glibc 2.3 or later (x86 and x64), Mac OS X 10.4 or later (x86 and powerpc), Solaris 2.8 or later (sparc, 32 and 64 bit), Solaris 10 or later (x86), Windows 2000 (x86), Windows XP, Server 2003, Vista, Windows Server 2008 (x86 and x64).

The principles outlined in this series will work with the latest version of ActivePerl. In this series there are many code samples that you will be trying.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Prerequisite
In order to study ActivePerl in this series, you should have basic knowledge in XHTML (or HTML). If you have not studied that language, then you should study it from the series I wrote in this blog (or another blog in my name). To arrive at the XHTML series that I wrote, type, “XHTML Basics” and my name, “Chrys” in the Search box of this page and click Search. Note: today, XHTML is like the latest version of HTML. XHTML is like strict HTML 4 with some additional features.

Also, in order to study ActivePerl and master it, your level of mathematics should be at least that of Middle School. If you did not pass in middle school mathematics, do not worry. There is a way out. You should study the mathematics course easily at the link below. This site offers online interactive middle school math course in a step-by-step fashion, in as short as three months or as long as 10 months. There you have the right to ask questions from the authors of the web site. You can be studying the math course while studying ActivePerl.

http://www.cool-mathematics.biz

Requirements
Here, I give you the requirements to study ActivePerl in this series.
- ActivePerl Interpreter
- Abyss Web Server X1
- Browser
- Text Editor
- Your Personal Computer and any of the operating systems mentioned above.

You can download ActivePerl Interpreter from http://www.ActiveState.com/ActivePerl free. Just search the web to download the personal web server, Abyss Web Server X1, free, that will work with your operating system.

Installation
They are both easy to install. Just follow the instructions and I suggest you choose the default settings. When you install the server, configure it to work with the ActivePerl Interpreter. Read the User’s Guide of the Abyss Web Server X1 server and the documentation of ActivePerl. You will not have to read the whole user’s guide for the server or the whole documentation for ActivePerl. Just read what is necessary in both documents to make the server and ActivePerl work together. In the next paragraph I tell you what I did in my own computer concerning the installation of these two applications. It will guide you.

I allowed all the default settings for ActivePerl. So the effective interpreter, which is perl5.10.1.exe was installed in the directory, C:\Perl\bin, giving C:\Perl\bin\perl5.10.1.exe . I did not do any configuration with the ActivePerl installation. I typed only too things for the server configuration as follows: In the field for the Interpreter, I typed, C:\Perl\bin\perl5.10.1.exe. In the field for the extension, I typed, pl. Any file having the extension, pl will be executed as a Perl (or ActivePerl) file.

In order to work with the server, you also need a login name and password.

Directories
Any HTML files will go into the directory, htdocs of your server. In my computer the operating system path for this directory is, C:\Program Files\Abyss Web Server\htdocs . It is in this directory that you will put your index.htm or index.html or default.htm or default.html file. This is your home server directory that corresponds to the host domain name.

You can create sub directories to this directory. Your ActivePerl script can be in the htdocs directory or any of its sub directories. For this series you will create a sub directory in the htdocs directory. You will call this sub directory, cgi-bin. It is in this directory that you will be putting the ActivePerl files you will be creating. ActivePerl files are called scripts or ActivePerl scripts.

Domain Name and URLs
Your domain name is localhost. So to access any HTML file in the home directory (htdocs), you will type, http://localhost/file.htm , where file.htm is an HTML file name. To access any ActivePerl script, you will type, http://localhost/cgi-bin/script.pl , where script.pl is your ActivePerl file name.

In my computer, these two links are http://localhost:8000/file.htm and http://localhost:8000/cgi-bin/script.pl . This is because Abyss Web Server X1 is not the default personal web server in my computer; I have another personal web server installed in my computer, and that one is my default server. In this series, you will have the :8000 after localhost, in all URLs. In order to test the sample scripts in your own computer, you will probable have to remove the “:8000” or maybe use some other number instead of :8000.

Your First ActivePerl Script
Type the following in your text editor:

use strict;

print "Content-Type: text/html\n\n";

print "<html>\n";
print "<head>\n";
print "<title>Hello</title>\n";
print "</head>\n";
print "<body>\n";
print "<h4>Hello World!</h4>\n";
print "</body>\n";
print "</html>\n";

Save the file with the name hello.pl in the directory cgi-bin you created in your server htdocs directory; do the saving using your operating system commands. Your server should always be running; so start it if it is not running. Next, to run the script, start your browser. Type, http://localhost:8000/cgi-bin/hello.pl in the address bar and click Go; you might need to omit the “:8000” or use some other number with the colon; see the documentation of your server for more information. This number is called the port for the web site. The default port number is “:80”, which is not usually typed. After you clicked, Go, a new page should display in your browser indicating, Hello World!.

The sample scripts in these series will be tested in this way. Do not forget to remove the “:8000” or use a different number when testing the sample scripts, or allow it if your situation is exactly like mine. See the documentation of your server for more information about the port number if necessary.

Explanation of the Script
The first line of the script is “use strict;” . With this statement, any error you make in your script will not be tolerated by the ActivePerl interpreter; this is actually a good thing. The next line is “print "Content-Type: text/html\n\n";” In order to send a new web page from the server to the brwser, you need this line.

The rest of the lines of the script begin with “print”. “print” is an example of what is called a function (or subroutine) in ActivePerl. What follows this function in quotes is called an argument. In each of the arguments above, you find HTML tags or text meant for the HTML page at the browser. Each function sends an HTML line to the browser. These lines are said to be sent in a stream.

Note that all the HTML tags in the arguments form an HTML web page code. Also note that before the end of each argument, you have the “n” character. This is called the ActivePerl newline character. The presence of these newline characters are not obligatory.

You can always view your HTML code when the page is displayed at the browser. To do that, right-click on the displayed page at the browser. A menu pops up. Click View Page Source or View Source or some similar command in the popup menu. You should see the HTML code displayed in some kind of text editor. If you try this for the above case, you should see the following code where each HTML tag is displayed on a separate line:

<html>
<head>
<title>Hello</title>
</head>
<body>
<h4>Hello World!</h4>
</body>
</html>

If in your hello.pl file, you did not have the newline (n) characters, you would see the following as the web page code, where each HTML tag is not on a separate line:

<html><head><title>Hello</title></head><body><h4>Hello World!</h4></body></html>

As you can see it is advisable to use the newline character as I did in the hello.pl file. Note that each line in the hello.pl file ends with a semicolon. Do not confuse between a column (:) and a semicolon (;).

From the hello.pl file, all the lines except the first arrive at the browser without the quotes. So the browser sees an HTML page and displays a web page. The argument, "Content-Type: text/html\n\n", tells the browser that what is coming next in the stream is an HTML page.

That is it for this part of the series. As I said above, all the code samples for this series will work in the tradition Perl Interpreter with its own operating system (Unix or Linux). For the traditional Perl and its operating systems, you will have to precede each of the code (samples) with something like, #!/usr/bin/perl . This is the path and directory to the effective Perl Interpreter called the Perl Executable.

Let us stop here and continue in the next part of the series.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
Perl Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message