Creating Perl Scripts


This chapter introduces the Perl interpreter NLM and describes

Function of the Perl Interpreter

The Perl interpreter is an NLM that uses RCGI to receive request data, invoke a Perl script, pass the request data to the script, interpret the script's output, and return the generated web pages to the NetWareŽ Web Server to transmit to the client. Click here to view a figure that illustrates this relationship.

The Perl interpreter must be loaded for Perl scripts to be interpreted. To load the interpreter, type:

load perl

The interpreter uses port 8002 by default. To define a port other than the default of 8002, use the -p switch when you load Perl. For example, to use port 8004, type

load perl -p 8004

Guidelines for Writing Perl Scripts

Before you write a Perl script that generates dynamic Web pages, you should:

  1. Learn about Perl and HTML.
  2. Plan the web pages to be generated by the script so you know what functions the script will perform.
  3. Write the HTML documents that represent the web pages.
  4. Build the Perl script around the HTML documents, embedding them in the script, if needed.
  5. Place the script on the server platform in the \WEB\SCRIPTS\PERL directory.
  6. Test the script from a web browser.

If you need detailed information about the Perl language, consult one of these texts.

Example of a Perl Script

The Web Server includes a sample script that shows how to use Perl to generate a dynamic Web page. The sample script is stored in the file \WEB\SCRIPTS\CARDSAMP.PL; Click here to view the script.

The script outputs two web pages: a static web page that allows a user to select one of several options and a dynamic Web page generated on the basis of the user's selection.

Writing the Script

The script implements a web page that offers users a choice of several greeting cards-a birthday card, an anniversary card, or a thank-you card-and asks users to specify their name and the name of the card's recipient. When the user chooses a card and enters the names, the server returns a new web page showing the chosen card and the personalized greeting. Such a script needs to accomplish the following:

  1. Include the Perl utility library.
  2. Print a header for the web page.
  3. Read the request data sent from the web browser and return an HTML page.

The following subsections describe the Perl statements that perform these functions.

Including the Perl Utility Library

The NetWare Web Server includes a library of Perl routines; click here to see the routines. To include a copy of this library with your C program, use this statement:

require("cgi-lib.pl");

Printing a Header

The statement shown below invokes the CGI library routine PrintHeader to print an HTTP header on the web page that is returned to the client.

print &PrintHeader;

The PrintHeader function defines the document being created as an HTML document by outputting the following line to the document:

Content-type: text/html\n\n

Reading the Request Data

To read the request data from the browser a script invokes the utility routine ReadParse to read stdin (if the request method was POST) or the query string (if the request method was GET). The script then converts it, and stores it in the list $in.

To invoke ReadParse, use the statement

&ReadParse;

Returning the Static Web Page

Next, the script returns the web page that allows the user to enter a greeting card choice and specify sender and recipient names. The statement

if ($ENV{"ARGC"} eq "0")

determines how many arguments were sent by the browser. Click here to see the statements the script executes to return the static HTML page if the argument count is zero.

Generating the Dynamic Web Page

If the number of arguments received by the script is not zero, this is a greeting card request. The ReadParse function has placed the returned argument values in the $in array. The script extracts the request type from the array (type 1 is a birthday card, type 2 is an anniversary card, any other value for type is a thank you card) as well as the names of the sender and recipient. Then the script prints out the HTML lines for the card. Click here to see the remainder of the script.

Using the Script

When a user accesses a script, the following actions are performed. (To access the sample script and follow the actions below, point your browser to CARD.PL.)

  1. The user types the URL of the greeting card Web page. For example, if the page is a NetWare Web Server named "webserver,"

    http://www.webserver.com/perl/card.pl

  2. The web browser sends this request to the server. The Perl interpreter invokes the script CARD.PL, which returns a static HTML page containing the card options.
  3. The user selects a greeting card option from the web page and enters the names of the sender and the recipient of the card.
  4. When the user clicks "Create Card," the web browser sends the request data, including the greeting card choice and the two names, to the same URL.
  5. The server relays the request message to the Perl interpreter, which invokes the script CARD.PL again. This time, the script skips the lines that return the static web page and instead generates a dynamic page displaying one of three cards and the arguments of two query strings: "who" and "name."
  6. The server returns the new page containing the requested card to the web browser, which displays it for the user.

Click here to see a figure that shows the Web page presented to the user who chooses the birthday card option.

The CARDSAMP.PL Script

Click here to view the sample script CARDSAMP.PL.

Supported Perl Statements

The Perl interpreter NLM supports most functionality provided by the Perl language. Exceptions include some functions that are valid only in the UNIX environment. Click on the links below to view tables that briefly describe the Perl statements supported by the Perl NLM.

System File Information

These Perl functions retrieve information about system files from three data structures. Click here to view the names of these data structures and their contents.

Click here to view a table of the system-file information functions that retrieve information from the protoent, hostent, and netent data structures.

Miscellaneous

Click here to view a table of miscellaneous functions.

Perl Script Library

The NetWare Web Server provides a library of Perl routines that you can use to manipulate CGI input and format output. Click here to view a table of these routines.

Unsupported Functions

Click here to view a table of Perl functions that are not supported by the NetWare Perl interpreter.