Changes

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 [http://www.ActiveState.com 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 '''test1.pl''' and '''test2.pl''' 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. Test1.pl uses the print function and prints every line individually. It encloses each line in quotes and each line requires terminating with a semi columnsemicolon.
|}
</td>
|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;"
|Test2.pl 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.
|}
</td>
</table>
'''''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;"
|-style="background:#f5f5f5;"
|<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/file_name.pl"''' 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 '''test3.pl''' , '''inc_header.pl''' , '''inc_content.pl''' and '''inc_footer.pl'''
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 '''test4.pl''' , '''header.pm''' , '''content.pm''' and '''footer.pm'''
|}
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 '''warnings.pm''', '''strict.pm''' more importantly '''lib.pm'''
'''lib.pm''' 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 (''header.pm'', ''content.pm'' and ''footer.pm'') into it. Make the following changes to test.4.pl and save as test5.pl
'''<nowiki>http://localhost/cgi-bin/test5.pl</nowiki>''' -- Displays test5.pl
There should be no differences in the displayed result between test 4test4.pl and test 5test5.pl all . All that has changed is the location of the three pms. Note: Test.4Test4.pl will fail because of the deleted pms.
'''''[[#top | Top]]'''''

Navigation menu