20.2 Server-Side PerlScriptPerlScript on the server-side of business is a much more viable method of incorporating Perl into your web applications. You can use PerlScript as the scripting language for Active Server Pages (ASP), which are used by such applications as Microsoft's IIS and O'Reilly's WebSite Pro. See the documentation for these products to properly set up ASP on your server. ActivePerl with PerlScript must be installed on the server machine to be used with ASP. Active Server Pages use PerlScript in pages much like client-side scripting except that the object model is different, and most importantly, the script is executed on the server. When an Active Server Page is requested, the .asp file is processed, scripts are executed, and an HTML page is produced and delivered to the client.
Each script is contained within The<SCRIPT LANGUAGE="PerlScript" RUNAT=Server>
RUNAT
attribute signals that the script is to be executed on the server
and not the client. The lines of the script follow, and the script is
closed with an ending
</SCRIPT>
tag.
An alternative syntax to the This delimiter syntax completely replaces the declarative<%@ PerlScript%>
<SCRIPT>
tag shown above, but it
must be placed at the top of any file using the
<% %>
syntax for scripting. In
addition to this tag, you can use the
<%= %>
tag throughout your file to
automatically assign a value to the output of your file (i.e., a
$Response->write
function). Here is an example:
The return value of any code within<%@ PerlScript> <HTML> <HEAD></HEAD> <BODY> <H1>Say Hello, Boys!</H1> <% my @onehit = ("Bel", "Biv", "DeVoe"); %> <P> <%= $onehit[0] %> says "Hello." <BR> <%= $onehit[1] %> says "Hello." <BR> <%= $onehit[2] %> says "Hello." </P> </BODY> </HTML>
<%= %>
is placed into the output HTML returned to the client.
This syntax makes it very easy to intersperse the output of commands with
HTML throughout the ASP file.
The scripting object model for ASP contains the following top-level objects:
The Request and Response objects provide an ASP interface to the HTTP protocol used in web transactions. The other objects encapsulate special server features and connections to outside applications. 20.2.1 The Request Object
The Request object contains all the information sent to the server in
the client's request. This object has only one property, TotalBytes, which
is read-only and gives the number of bytes sent in the body of a client request.
The The information contained in a request is stored in various collection objects. Collections contain objects that represent the important pieces of a request, for example, form or cookie data. There are five collections under the Request object:
To access the objects in the collection, the name of an object is given as the argument to the Collection object.
For example, to access a form variable named This returns an object for the$object = $Request->Form("birthday");
"birthday"
form variable. To get the value, you use
the
Item
method on the object:
This returns the value assigned to$data = $Request->Form("birthday")->Item;
"birthday"
in the
request. Since ASP collection objects are OLE collections, the
functionality of the Win32::OLE modules can be employed. See
Chapter 19,
Win32 Modules and Extensions
, for more information on OLE collections.
The QueryString collection contains form data input by the GET method and transmitted at the end of the requested URL. Values are retrieved from the QueryString object the same way they are retrieved from the form object, for example: Cookies sent within a client request are stored the same way. The name of the cookie variable retrieves the cookie object from the collection, and Item retrieves the value:$data = $Request->QueryString("birthday")->Item; The ServerVariables collection stores both environment variables relevant to the transaction and HTTP headers sent in the request. The header objects are accessible by using the syntax: HTTP_ HeaderName . Underscores contained within HeaderName are interpreted as dashes.$data = $Request->Cookie("birthday")->Item; 20.2.2 The Response Object
The Response object contains the information output to the client.
The 20.2.2.1 Setting cookiesThe Response object contains one collection for setting cookies with the client. The Cookies collection allows you to create cookies and sets the information that is delivered to the client in the Set-Cookie header of the HTTP response. In the ASP file, you can check for a returned cookie in the request, and if there is none, you can set a new cookie: This will check to see if a cookie named<% if ( defined($Request->Cookie("user") ) { $userid = $Request->Cookie("user")->Item; } else { $Response->Cookie("user") = 123; }; %>
user
was sent in the
request. If it was not, a new cookie is sent in the response and set
on the client machine.
The Response cookie collection uses a number of properties to set the standard attributes used with cookies. For example, to set the expiration of a cookie, you would use the Expires attribute: The client will no longer return a cookie after its expiration date.$Response->Cookie("user")->{Expires} = "Tuesday, 31-Dec-99 00:00:00 GMT"; The following properties are used to set attributes on a response cookie:
20.2.2.2 Response propertiesThe following properties can be set on the Response object. The property syntax is as follows: $Response->{ property } = value ;
20.2.2.3 Response methodsThe following methods can be used on the Response object.
Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|