Perl Application Server Developer's Guide | ||
---|---|---|
<<< Previous | Next >>> |
Pas server pages are modled as text files with embedded Perl code in them. They are typicly used to produce HTML, though they are capable of producing any MIME content type including, but not limited to, text/xml, text/plain, image/jpg, and image/png.
PSPs use a syntax very similar to that of other embeded HTML languages. The embedded Perl code is surounded by <% and %> tags. Any content in the PSP file that is not within the <% and %> tags will not be processed by the PSP compiler. Any such content will become part of the output produced when the PSP is executed.
PSP by itself is really very simple. The fastest way to get familliar with it is to just start with an example. This example displays the current date and time:
<HTML> <BODY BGCOLOR="#FFFFFF"> <H1>The Current Time</H1> <P> The current time is: <%= scalar(localtime()); %> </P> </BODY> </HTML> |
Past the obvious similarity to other embeded HTML languages, PSP begins to diverge. PSPs are compiled into a Pas Page object. The content of the PSP page is then reqpresented in the Page object's execute method. This methodology of compiling the PSP into a Perl program which is executed to produce the output of the PSP is one of the strengths of the PSP system. This approach takes full advantage of Perl's parser and syntax checking. Errors are reported relative to the location of the code PSP page instead of an anonymous evaluated expression, as with some other embeded Perl in HTML toolsets.
There are two major types of PSP tags. Page directives and scripting tags. All PSP tags have the form <% ...code... %>. Page directives begin with an @ character, while scripting tags can start with any of several characters..
<%-- This is a PSP Comment --%> <!-- This is an HTML, XHTML, or XML comment --> |
Though two types of comments are given here as examples, it is only the first example that is actualy a PSP comment. PSP comments are stripped from the document at compile time. They do not become part of the output produced by the page object, and in fact do not end up in the compiled Page object. The HTML comment is treated as any other content in the PSP page and will become part of the output of the PSP.
HTML comments need not be static data, if you wish to embed information in the document that does not render as part of the displayed page, you could include it as a comment.
<!-- This document was generated on: <%= scalar(localtime) %> --> |
<%! my $foo = 'bar'; %> <HTML> <BODY> foo: <%= $foo %> </BODY> </HTML> |
Variable declarations declare variables with local scope in the PSP page. It is not necessary to declare variables using this syntax, as you can declare them anywhere within a scriptlet. Variables will remain in scope from the point of declaration on until the end of the enclosing scope.
<HTML> <BODY> 2 + 3 = <%= 2 + 3 %> <BR> The time is: <%= scalar(localtime) %> </BODY> </HTML> |
Expressions are replaced by the last value produced by the code in the expression. Expressions are executed at request time to produce values which are included in the output of the PSP. In the above example the expression produces a string representing the current date and time.
<HTML> <BODY> <% my $firstName = $self->query->param('firstName'); %> <H1>Logicly, this is a test</H1> <% if($firstName) { %> Welcome, <b><%= $firstName %></b>. <% } else { %> I'm sorry, you don't seem to have a first name. <% } %> </BODY> </HTML> |
The scriptlet construct allows the declaration of variables, as well as the inclusion of branching logic into the PSP page. Scriptles may span content areas. This allows you to conditinaly emit content. Scriptlets can also be to prepare data that will be emitted by an expression.
<HTML> <BODY> Account status for: <%= $self->displayName() %> <BR> <HR> ... </BODY> </HTML> <%[ sub displayName { my($self) = @_; my $first = $self->query->param('firstName'); my $middle = $self->query->param('middleName'); my $last = $self->query->param('lastName'); return "$last, $first $middle"; } %> |
Method declarations will add a new method to the generated page object, which can then be called from within the PSP. This tag can also be used to override methods from the parent object of the PSP page. You can not use this method to override the execute() method of the current PSP page.
<%@ page extends = "Some::Class" %> |
The extends directive changes the parent object that the PSP will be derived from. A class specified in an extends directive must be derived from Org::Bgw::Pas::Page, which is the default parent if extends is not specified in a PSP file.
<%@ page import = "Some::Class" %> |
The import adds a use Some::Class to the page object the PSP is compiled into. This directive gives you one avenue for making use of other Perl modules.
<%@ include file = "path/to/file.psp" %> |
This directive staticly includes one PSP file in-line into another. The entire contents of the included file is inserted into the including file in the exact location of the include directive.
<%@ page language = "perl" %> |
In the current version of PSP, this directive does nothing. If the directive is present and the specified language is not perl, a compiler error is generated.
<%@ page session = "(true|false)" %> |
Logically this directive sets your page up for session access. In reality this directive affects the generated Page object's ISA by appending in Org::Bgw::Pas::SessionPage.
<%@ page buffer = "(none|siebytes|sizeK|sizeM)" %> |
In the current implementation of Pas this directive has no effect. The current implementation of Pas buffers the entire response during the request process. In future versions this directive will affect how much data is buffered before data is flushed from the buffer and sent to the client.
<%@ page autoFlush = "(true|false)" %> |
In the current implementation of Pas this directive has no effect.
<%@ page isThreadSafe = "(true|false)" %> |
In the current implementation of Pas this directive has no effect.
<%@ page info = "descriptive text" %> |
<%@ page errorPage = "relative/url" %> |
In the current implementation of Pas this directive has no effect.
<%@ page contentType = "mimeType [;charset=characterSet" %> |
This directive sets the Content-type HTTP header that will be returned to the client. You should be careful, if you decide to use this directive, to make sure your PSP actualy produces the mime type specified by this directive. The default type is text/html.
<%@ page isErrorPage = "(true|false)" %> |
In the current implementation of Pas this directive has no effect.
<%@ page package = "Overriding::Packae::Name" %> |
This directive absolutely sets the package name for the Page object generated from the PSP. You must take special care when using this directive so the names you use do not clash with other objects on your server. The default value for the generated package name is computed from the absolute file path to the PSP file and a prefix specific to Pas, which produces package names that are uniqe for a given PSP file.
<<< Previous | Home | Next >>> |
Introduction | PSP Environment |