487
edits
mNo edit summary |
(Fix grammatical stuff and clarify meaning) |
||
Line 16: | Line 16: | ||
'''Quick Perl (CGI) 3.5-Apollo''' | '''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 | 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 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 is not intended in any way to teach you Perl. 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 | 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 link above. | ||
== Introduction == | == 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. | 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]]''''' | '''''[[#top | Top]]''''' | ||
== Perl Syntax == | == Perl Syntax == | ||
The following are very basic HTML pages producing identical output. They show how to use Perl | The following are very basic HTML pages producing identical output. They show how to use Perl. Save the pages as '''test1.pl''' and '''test2.pl''' 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: | Type the following into your browser address bar: | ||
Line 49: | Line 49: | ||
<nowiki>exit (0);</nowiki><br> | <nowiki>exit (0);</nowiki><br> | ||
|- style="background:#e6e6e6;" | |- style="background:#e6e6e6;" | ||
|Every Perl page must have the first line as shown | |Every Perl page must have the first line as shown. 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;" | |- 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 | |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 semicolon. | ||
|} | |} | ||
</td> | </td> | ||
Line 76: | Line 76: | ||
|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. | |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;" | |- style="background:#e8e8e8;" | ||
|Test2.pl uses a different style that allows you to print a block of text retaining its format. This is referred to as “here document” 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. | |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> | </td> | ||
Line 82: | Line 82: | ||
</table> | </table> | ||
'''''Note:''''' Older versions of Perl use '''\n\n''' instead of '''\r\n\r\n''' | '''''Note:''''' Older versions of Perl use '''\n\n''' instead of '''\r\n\r\n'''. For Windows '''\n\n''' is the correct one to use since it is more compliant. | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
== Page building == | == Page building == | ||
With Perl's print command you are building a page from scratch | With Perl's print command you are building a page from scratch. The resulting page is passed onto the server. Looking at the above script in more detail: | ||
{| cellpadding="4" cellspacing="1" style="background:#000000;" | {| cellpadding="4" cellspacing="1" style="background:#000000;" | ||
Line 122: | Line 122: | ||
|-style="background:#f5f5f5;" | |-style="background:#f5f5f5;" | ||
|<nowiki>exit (0);</nowiki> | |<nowiki>exit (0);</nowiki> | ||
|After the Perl script ends, Apache collects the "return value" of the program | |After the Perl script ends, Apache collects the "return value" of the program. 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: | In Perl, you can specify what the return value of a CGI program with the exit keyword: | ||
Line 131: | Line 131: | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
== Include external files -1 == | == Include external files -1 == | ||
Perl has so many ways of doing this | Perl has so many ways of doing this 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 of these files set functions and or variables, nothing clever, it just means I have one file structure. To give you an idea what I am talking about, 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: | 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: | ||
Line 171: | Line 171: | ||
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''' | 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 | I have highlighted the additional content required to create each file. The main page uses '''require''' to load each file. These statements are place just below the shebang. The advantage of this method is I can use file names that indicate the functions they contain. Once all three files are loaded I have three functions that can be used anywhere in my script. | ||
I call each function in turn using its name and a pair of | I call each function in turn using its name and a pair of parentheses, for example header() will print the header content. | ||
{| cellpadding="4" cellspacing="1" style="background:#000000;" | {| cellpadding="4" cellspacing="1" style="background:#000000;" | ||
Line 229: | Line 229: | ||
|} | |} | ||
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 | 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: | Type the following into your browser address bar: | ||
Line 238: | Line 238: | ||
== Include external files -2 == | == 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 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 | 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 include file has an extension of .pm indicating a Perl module; its content 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''' | 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''' | ||
Line 287: | Line 287: | ||
|} | |} | ||
There are restrictions, '''package name''' must match the '''file name''' and the file must have an extension of '''.pm''' | There are restrictions, '''package name''' must match the '''file name''' and the file must have an extension of '''.pm''' . For this example all files are placed in the cgi-bin folder. | ||
Type the following into your browser address bar: | Type the following into your browser address bar: | ||
Line 296: | Line 296: | ||
== Perl Modules == | == 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''' | 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 | '''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 | ||
Line 325: | Line 325: | ||
'''<nowiki>http://localhost/cgi-bin/test5.pl</nowiki>''' -- Displays test5.pl | '''<nowiki>http://localhost/cgi-bin/test5.pl</nowiki>''' -- Displays test5.pl | ||
There should be no differences in the displayed result between | There should be no differences in the displayed result between test4.pl and test5.pl. All that has changed is the location of the three pms. Note: Test4.pl will fail because of the deleted pms. | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' |