start page | rating of books | rating of authors | reviews | copyrights

Book HomeJava and XSLTSearch this book

8.38. CGI::Pretty

Allows users of CGI to output nicely formatted HTML code by adding carriage returns and indentations to HTML markup for easy readability. For example, the following code:

#!/usr/local/bin/perl -w

use CGI qw(:all);

my $query = CGI->new( );

print $query->start_html( );
print $query->table( TR( td( "foo" ) ) );
print $query->end_html( );

outputs the following HTML:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"
        "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head><title>Untitled Document</title>
</head><body><table><tr><td>foo</td></tr></table></body></html>

which is ugly. You can make it prettier with CGI::Pretty:

#!/usr/local/bin/perl -w

use CGI::Pretty qw( :html3 );

my $query = CGI->new( );

print $query->start_html( );

# Print a table with a single data element
print $query->table( TR( td( "foo" ) ) );

print $query->end_html( );


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.