Article

Home » Server-side Coding » CGI & Perl Tutorials » Embedding Perl Into Web Pages

Embedding Perl Into Web Pages

By Wrox Press

September 4th, 2001

Reader Rating: 5

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

518coversmallThis article is an excerpt from the book Professional Perl Development from WROX Press. See Kevin Yank's review here.

The exponential growth of the web in recent years has led to a number of conflicting demands on a site's development team. On the one hand, users expect more than simple static content โ€“ they demand interactivity through forms, search facilities, customization of pages, and so on. At the same time, however, users anticipate such things as an aesthetically pleasing layout, easy navigation, and a similar look and feel among related pages. These demands in fact underline a fundamental direction that the Web is heading in โ€“ a separation of content from presentation. This separation in principle can make development of a site easier; first handle the generation of dynamic content, and then address how the results are to be presented. Ultimately however, one has to make an interface between the two โ€“ how to input the dynamic data into an HTML page.

In many cases, such as a search facility that looks up a query in some database, this interface can be handled through CGI scripts. Of course, Perl, and in particular the CGI.pm module (see Chapter 1), is justifiably famous for this. So much so that many bookstores, web sites, and news group posters consider Perl and CGI programming to be synonymous (much to the chagrin of, among others, regular posters to comp.lang.perl.misc). However, for more complicated applications and/or for large numbers of pages, an approach based on CGI scripts is not always satisfactory for a number of reasons:

  • It does not always scale well.
  • It can place large demands on the server.
  • It can be difficult to manage for large sites.

A primary reason for these shortcomings is that the separation of content and presentation is not always so clean in a CGI script. Indeed, often it is just one script that generates both the content and the HTML tags, which makes it difficult to change the presentation or the content individually. There are, however, a number of different classes of solutions available within Perl that were developed to address this problem:

  • HTML::Template
  • Template
  • HTML::Mason
  • HTML::Embperl
  • Apache::ASP

These approaches will be the main subject of this chapter. All of these modules are available on CPAN, and for UNIX can be installed in the usual ways. In a Win32 environment we may need an expensive C compiler to build and install them (particularly Visual C++ if we would also like to use them with mod_perl, described in Chapter 2). If we don't have VC++, pre-built binaries of these modules are available through links listed at http://perl.apache.org/, including ppm (Perl Package Manager) files for Win32 ActivePerl.

Of course, constructing web pages can be handled purely from a CGI/mod_perl approach without the use of modules, such as those that will be described here for embedding Perl in a web page. The advantages of using these modules is that they are designed specifically for a web environment, and so have many optimal constructions and shortcuts built in that have been found very useful in this context. Also, many users have tested these modules extensively in this environment. Once we learn the syntax of a particular module, we are able to write components that are easily maintainable, reusable, and scalable and this has distinct advantages.

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

Sponsored Links