The Sysadmin Notebook  

Sitemap

Perl Web Scripts

Generating html from Perl

Contents

Generating html from scripts allow you to alter content at runtime in response to user-entered parameters or external data sources, such as cookies, databases, the content of external sites or runtime environment variables. Once your WebServer is configured to execute scripts, you can try the scripts below

A simple example using a perl script:

#!/usr/bin/perl
use strict;
use warnings;

print "Content-Type: text/plain\n\n";
print "Hello World!";

CGI

Top Bottom

Using the CGI module:

#!/usr/bin/perl
use strict;
use warnings;
use CGI;

my $cgi = CGI->new();
print $cgi->header;
print $cgi->start_html(title => 'Hello');
print $cgi->h1("Hello World");
print $cgi->end_html();

Or using alternative CGI syntax:

#!/usr/bin/perl
use CGI qw/:standard/;
print header, start_html('Hello World'),
	h1('Hello World'),
	end_html;

Template Toolkit

Top Bottom

A simple scipt using Template Toolkit:

#!/usr/bin/perl
use strict;
use warnings;
use Template;

my $template = Template->new();
my $stash = {
	title => 'Hello',
	message => 'Hello World!'};
$template->process(\*DATA, $stash);

__DATA__
Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>[% title %]</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
  <h1>[% message %]</h1>
</body>
</html>

A more complex TT script:

#!/usr/bin/perl
use strict;
use warnings;
use Template;

my $template = Template->new();
my @names = qw/James Jane Dave John/;
my $stash = {
	title => 'Iterator',
	message => 'Hello World!',
	iterate => \@names};
$template->process(\*DATA, $stash);

__DATA__
Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>[% title %]</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
  <h1>[% message %]</h1>
<p>and a big hello to
[% FOREACH name IN iterate -%]
[%- name %]
[%- IF name != iterate.last %] and [% END -%]
[%- END %]
</body>
</html>

The 'complex' TT template executed using FastCGI:

#!/usr/bin/perl
use strict;
use warnings;
use FCGI;
use Template;

my $template = Template->new();
my @names = qw/James Jane Dave John/;
my $stash = {
	title => 'Iterator',
	message => 'Hello World!',
	iterate => \@names};
while (FCGI::accept >= 0) {
	$template->process(\*DATA, $stash);
}

__DATA__
Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>[% title %]</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
  <h1>[% message %]</h1>
<p>and a big hello to
[% FOREACH name IN iterate -%]
[%- name %]
[%- IF name != iterate.last %] and [% END -%]
[%- END %]
</body>
</html>

XML

Top Bottom

A script to load an XML data file using XML::Parser.

The input XML file looks like this:

<?xml version="1.0" ?>
<books info="Some Interesting Perl Books">
  <book>
    <title>Perl Cookbook</title>
    <author>Tom Christiansen, Nathan Torkington</author>
    <publisher>O'Reilly</publisher>
    <rating>10</rating>
  </book>
  <book>
    <title>Perl Best Practices</title>
    <author>Damian Conway</author>
    <publisher>O'Reilly</publisher>
    <rating><?perl int(rand(10)) ?></rating>
  </book>
  <book>
    <title>Programming Perl</title>
    <author>Larry Wall, Tom Christiansen, Jon Orwant</author>
    <publisher>O'Reilly</publisher>
    <rating>9</rating>
  </book>
</books>

and the script to transform it looks like this:

#!/usr/bin/perl
use strict;
use warnings;
use XML::Parser;

my $parser = XML::Parser->new();
$parser->setHandlers(	Start 	=> \&start,
			End	=> \&end,
			Char	=> \&char,
			Proc	=> \&proc,
		);
my $header = &getXHTMLHeader();
print $header;
$parser->parsefile('books.xml');

my $currentTag = "";

sub start() {
	my ($parser, $name, %attr) = @_;
	$currentTag = lc($name);
	if ($currentTag eq 'books') {
		print "<head><title>". $attr{'info'} . "</title></head>";
		print "<body><h2>" . $attr{info} . "</h2>";
		print '<table summary="' . $attr{info} . '"><tr><th>Title</th><th>Author</th><th>Publisher</th><th>Rating</th></tr>';
	}
	elsif ($currentTag eq 'book') {
		print "<tr>";
	}
	else {
		print "<td>";
	}
}
sub end() {
	my ($parser, $name, %attr) = @_;
	$currentTag = lc($name);
	if ($currentTag eq 'books') {
		print "</table></body></html>";
	}
	elsif ($currentTag eq 'book') {
		print "</tr>";
	}
	else {
		print "</td>";
	}
}
sub char() {
	my ($parser, $data) = @_;
	print $data;
}
sub proc() {
	my ($parser, $target, $data) = @_;
	if (lc($target) eq 'perl') {
		$data = eval($data);
		print $data;
	}
}
sub getXHTMLHeader() {
	#my $header = '<?xml version="1.0" encoding="UTF-8" ?>
	my $header = 'Content-Type: text/html

	<!DOCTYPE html
	  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
	return $header;
}