Home > Articles > Web Services > XML

Embedding Perl in HTML or XML

  • Print
  • + Share This
Chris Radcliff shows you how to change programs from code-centric to Web-centric using Perl embedded in HTML or XML. Web application languages such as PHP and Java Server Pages (JSP) have shown that embedding code in a Web page is a popular concept. What does it offer the Perl programmer or Web designer? The "Hello World" syndrome, Web usability, and code separation are discussed.
This article is excerpted from Perl for the Web, by Chris Radcliff.

To put it simply, embedding Perl in Hypertext Markup Language (HTML) (or Extensible Markup Language [XML]) documents avoids simplistic application designs. If the focus of an application is what the Perl code is doing instead of how the result is presented to the user, that result is likely to be too simple to convey the full meaning of the data being presented. In an HTML environment, the end product is everything because it's the only thing the user sees.

Basing a program on HTML makes it more Web-like and takes advantage of Web techniques. For instance, a Web application has the capability to reference parts of the same application (other pages) or parts of other Web applications indiscriminately. The point of a hypertext system is to link all possible applications (or pages) in a web of nodes that have no boundaries. If a Web application is designed as a monolithic structure, it's less likely to take advantage of these links to communicate with anything but itself.

After the focus is shifted from traditional applications to Web-centric applications, it's possible to ease development further by adding template mechanisms and convenience methods. The need for reusable components becomes much more acute when hundreds of programs with minor differences are being created to operate as a whole within a coherent environment. Because Web users value consistency from one site to the next and insist on consistency within a site, it makes perfect sense to separate out those parts of a site that stay constant and reference them indirectly within each individual program. From there, it's only a short hop to writing the programs themselves as components that are referenced indirectly by a user's actions as he or she moves through a dynamic site.

"Hello World" Syndrome

The first Web application learned by most Common Gateway Interface (CGI)-style developers is nearly identical to the first application learned by any other programmer—"Hello World." Listing 1 is an example of how the Hello World program might be created by a Web programmer who is beginning to learn Perl CGI.

Listing 1-Hello World

01 #!/usr/bin/perl
03 # include libraries
04 require 5.6.0;
05 use strict;
06 use warnings;
07 use CGI;
09 # initiate CGI parser object
10 my $q = CGI->new;
12 # begin the page
13 print $q->header, 
14    $q->start_html('Hello World');
16 print $q->p("Hello, World!");
18 print $q->end_html;

This program might look pretty impressive from a Perl point of view. In a Web context, though, it's one line of relevant information and 18 lines of telling us what we already know—we're using Perl, we're writing a Web application, and it's creating HTML. Its output probably would inspire no one:

Hello, World!

As it's written, the program produces a very simple page. It doesn't take advantage of HTML's more advanced features. However, the simple case still is difficult enough that more complex tasks (such as the compilation of information from around the Web by a page like My Yahoo!) seem daunting in comparison to their HTML output. In fact, as the program gets more complex, it still finds no room for the frills, color, and ease of use users have come to expect on the Web. As a result, too many mature applications on the Web show the same blandness from which Hello World suffers.

Worse yet, future Web applications written in the same style have to start from Hello World all over again because none of the code developed for one application is exported into the environment for other applications to use. This provides no continuity at all over the course of a site, and it relegates usability concerns to the few Web programmers who have successfully slogged through the reams of code necessary to get the programs working in the first place.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.