Article

Embedding Perl Into Web Pages

Page: 1 2 3 4 5 6 7 8 9 10 11 Next

'Apache::ASP'

Apache::ASP is an implementation of Active Server Pages (see Chapter 9 for more on ASP) for the Apache web server using Perl as the scripting engine – for more details, see the web site at http://www.apache-asp.org/. After installation of the module, directives such as the following samples are inserted into Apache's httpd.conf file:

PerlModule Apache::ASP          
Alias /asp/ "/home/www/asp/"          
<Location /asp>          
   SetHandler perl-script          
   PerlHandler Apache::ASP          
   PerSetVar Global /tmp          
   PerlSetVar CookiePath /          
</Location>

This assumes a mod_perl-enabled server. Any file placed in the /asp directory will be parsed by the Apache::ASP Apache handler.

As with Mason and Embperl, we give the simple example of printing out the values of the various environment variables:

<!--#include file=header.inc-->          
<H3>Environment Variables</H3>          
<CENTER>          
<TABLE BORDER=1>          
<TR><TH COLSPAN=2 ALIGN="left">Environment Variables</th></tr>          
<% @colors   = ("#FFFFFF", "#CCCCCC");          
   $x = 0;          
   sub _color{          
      return $x++ % 2 ? $colors[0] : colors[1];          
   }%>          
<% for(sort keys %{$Request->ServerVariables()}) {          
   next unless /HTTP|SERVER|REQUEST/; %>          
   <TR BGCOLOR=<%=_color()%>>          
   <TD><TT><%=$_%></TT> </TD>              
   <TD><TT><%=$Request->ServerVariables($_)%></TT></TD>          
   </TR>          
<% } %>          
</TABLE>< /CENTER>          
<!--#include file=footer.inc-->

The invoked file, header.inc, is:

<%          
   $title = 'My Apache::ASP demo'; %> <HTML>          
<HEAD>          
<TITLE><%=$title%></TITLE><          
/HEAD>          
<BODY BGCOLOR="#ffffff" link="#ff5599" vlink="#993399">          
<BR />

The footer.inc file is given by:

<P>          
Comments to          
<A HREF="mailto:me@my.address.com">me</a>          
are welcome.          
</BODY>          
</HTML>

In Apache::ASP, Perl code is enclosed within <%...%> blocks. Variables to be printed out use the <%=$variable_name %> syntax. The environment variables themselves are contained within $Request->ServerVariables – note that we restrict the variables to match the regular expression /HTTP|SERVER|REQUEST/. If the file above is called environ3.html, a screenshot of the result of calling http://localhost/asp/environ3.html appears below:

518environmentalvariables

In the above example, $Request->ServerVariables was used to access the server environment variables. As the syntax indicates, $Request is an object and ServerVariables is a method available for that object. Apache::ASP supports a number of such (global) objects; the current ones available are:

  • $Session – user session state
  • $Response – output to browser
  • $Request – input from browser
  • $Application – application state
  • $Server – general support methods

There are many methods available for each of these objects – consult the documentation for a full description.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links