Jump to navigation Jump to search

New Users: Quick Perl CGI

37 bytes added, 05:23, 28 January 2010
Fix grammatical stuff and clarify meaning
'''Quick Perl (CGI) 3.5-Apollo'''
So you have installed Uniform Server, performed all tests and it’s working. Where is the on off switch for PERL? The answer is straightforward, it’s already running, any . Any page with a '''.pl''' extension placed in folder '''*\Uniform Server\udrive\cgi-bin''' is routed to the Perl compiler and executed. I have written this page purely for orientation with reference to Uniform Server, it . It is not intended in any way to teach you Perl you . You can find better material on the Internet. Its sole purpose to get you up and running with Perl on Uniform Server 3.5-Apollo.
There are two faces of Perl, ; CGI for dynamic web pages and CLI for batch programming. This page covers CGI, ; if you are interested in CLI , check out the linkabove.
== Introduction ==
The Perl engine installed on Uniform Server is version v5.8.8 provided by [ ActiveState] it is not only very powerful but extremely compact. If you have read [[New Users Quick PHP CGI | Quick PHP CGI]] , I have tried to make quick Perl's content similar.
'''''[[#top | Top]]'''''
== Perl Syntax ==
The following are very basic HTML pages producing identical output. They show how to use Perl, save . Save the pages as '''''' and '''''' place by placing both in folder '''*\Uniform Server\udrive\cgi-bin'''. All your Perl pages are placed in this (cgi-bin) folder.
Type the following into your browser address bar:
<nowiki>exit (0);</nowiki><br>
|- style="background:#e6e6e6;"
|Every Perl page must have the first line as shown it . It is referred to as the shebang . '''<nowiki>#!/usr/bin/perl</nowiki>''' informs Apache where to find Perl. Make sure there are no spaces before # otherwise the path will not be found.
|- style="background:#e8e8e8;"
|The difference between the two programs is in terms of style. uses the print function and prints every line individually. It encloses each line in quotes and each line requires terminating with a semi columnsemicolon.
|Every web page must have a Content-type of text/html of great importance are the next characters '''\r\n\r\n''' they terminate the header.
|- style="background:#e8e8e8;"
| uses a different style that allows you to print a block of text while retaining its format. This is referred to as “here document” and starts with two less-than characters and a word; this word is also used to delimit the block and must not have a semicolon after it.
'''''Note:''''' Older versions of Perl use '''\n\n''' instead of '''\r\n\r\n''' for . For Windows '''\n\n''' is the correct one to use since it is more compliant.
'''''[[#top | Top]]'''''
== Page building ==
With Perl's print command you are building a page from scratch the . The resulting page is passed onto the server. Looking at the above script in more detail:
{| cellpadding="4" cellspacing="1" style="background:#000000;"
|<nowiki>exit (0);</nowiki>
|After the Perl script ends, Apache collects the "return value" of the program it . It then knows if there was an error or not. If the return value is zero, everything is okay. If the return value is not zero, Apache will display an error and log the error to the log files.<br>
In Perl, you can specify what the return value of a CGI program with the exit keyword:
'''''[[#top | Top]]'''''
== Include external files -1 ==
Perl has so many ways of doing this as to that what method you choose depends on your application. Coming from PHP you most likely to want to include headers, footers, functions and configuration data. My preferred method is to use '''require "some_path/"''' each . Each of these files set functions and or variables, nothing cleaver clever, it just means I have one file structure. To give you an idea what I am talking about lets , let's look at an example.
Suppose I want to use this master page as a template with header, content and footer. The first thing to do is chop it up into the appropriate parts:
Convert the above into four files as follows: (Save the pages as as '''''' , '''''' , '''''' and ''''''
I have highlighted the additional content required to create each file. The main page uses '''require''' to load each file these . These statements are place just below the shebang. The advantage of this method, is I can use a file names that are different to indicate the functions they contain. Once all three files are loaded I have three functions that can be used any where anywhere in my script.
I call each function in turn using its name and a pair of bracketsparentheses, for example header() will print the header content.
{| cellpadding="4" cellspacing="1" style="background:#000000;"
Each of the included files can be named anything you like so long as they have the .pl extension. Contents of each file in this example is one function. Each function (sub) has a unique name, and use the short-cut print command to print the HTML we require. At the end of each file you must include '''1'''; this keeps Perl happy, ; it indicates the content is valid.
Type the following into your browser address bar:
== Include external files -2 ==
Looking at the above you can see our template page is modular, uses three files, each containing a small package of information to create the final page. Perl modules and packages allow you to directly use a file name without the extension in a require statement. What is really neat is you can then use that require name in any location and the code is inserted, similar to PHP's include. The above example requires minor modifications, each . Each include file has an extension of .pm indicating a Perl module ; its content are is defined as a module.
The following shows how to convert the above into module files: Save the pages as as '''''' , '''''' , '''''' and ''''''
There are restrictions, '''package name''' must match the '''file name''' and the file must have an extension of '''.pm''' for . For this example all files are placed in the cgi-bin folder.
Type the following into your browser address bar:
== Perl Modules ==
Where are Modules Installed? On Uniform Server , Perl looks in the folder '''*\Uniform Server\udrive\usr\lib''' in this folder you will find common pms such as '''''', '''''' more importantly ''''''
'''''' Allows you to define a location for your own Perl modules. For example create a folder '''my_pms''' in the library folder '''*\Uniform Server\udrive\usr\lib''' and copy the above three pms ('''', '''' and '''') into it. Make the following changes to and save as
'''<nowiki>http://localhost/cgi-bin/</nowiki>''' -- Displays
There should be no differences in the displayed result between test and test all . All that has changed is the location of the three pms. Note: will fail because of the deleted pms.
'''''[[#top | Top]]'''''

Navigation menu