Using Perl Extensions with SSIThe Zeus Perl Extensions offer a new SSI tag: #perl. This can be used as a simple way to embed the output of a Perl subroutine in an HTML page.
<html>
<head><title>Calling Perl from SSI</title></head>
<body>
<!--#perl sub="sub { print \"hello, world!\"; }" -->
</body>
</html>
When the SSI tag is evaluated by the server, it calls the Perl runner and asks it to evaluate the Perl code in the sub argument. Anything that this code prints to standard output will then be included into the document. Naturally, you should make sure to enable this capability only on virtual servers whose content authors are trusted. Consider a more complex example. A hosting company wants to insert random advertising banners into a set of pages that already use SSI. Each banner needs to be associated with an appropriate link. To do this, they write a simple configuration file containing image and link URLs, one pair per line, and use the following Perl handler to select a random pair to display.
package RandomLink;
use strict;
use Zeus::ModPerl;
use Zeus::ModPerl::Constants qw( OK );
sub handler
{
my $r = shift( @_ );
my $source = shift( @_ );
# Come up with a random image/link combination from the
# configuration file $source.
local( *CONFIG );
open( CONFIG, '<', $source ) or return OK;
my @config = <CONFIG>;
close( CONFIG );
# choose a line at random from the list:
my $line = $config[ int(rand(@config)) ];
# bail out if the line doesn't look right:
# <image url> <whitespace> <link url>
$line =~ /(.*?)\s*(.*)/ or return OK;
my( $image, $link ) = ( $1, $2 );
$r->print( qq{<a href="$link"><img src="$image"></a>\n} );
return OK;
}
1;
When this code is put in $ZEUSHOME/zperl/local/lib/site_perl/RandomLink.pm, the .shtml pages can then invoke the handler like this: <!--#perl sub="RandomLink" arg="/home/www/ads.config" -->
Content Manager
[Administrator] 16 December 2005
|
Recent Articles
Other Resources
|


