Press Releases with Mason

To learn more about building dynamic web sites, Mr. Lerner presents an application for reading the news using Mason and MySQL.
Retrieving News

While we could retrieve the news directly into a top-level component, it is easier for us to create a generic component that retrieves any number of articles from any category. In this way, we can use this “get-news.comp” component in a number of different high-level components, retrieving the number and type of articles that interest us.

Listing 6

Listing 6, get-news.comp, is fairly straightforward, returning a list of articles to the caller. It builds the article list much as we built the category list in add-news-form.html, retrieving each of the articles:

while ($row_ref = $sth->fetchrow_arrayref)
{
     my ($headline, $body, $posting_date) =
           @$row_ref;
     push @articles, {headline => $headline,
           body => $body,
           posting_date => $posting_date};
}
return @articles;

We take advantage of MySQL's LIMIT clause to restrict the retrieval to only as many articles as the user is interested in receiving. Also, we retrieve the articles in reverse order of their arrival, so that the article with the latest timestamp will come first. This ensures whenever we retrieve the latest five articles, they will indeed be the newest:

my $sql  = "SELECT headline, body, posting_date ";
   $sql .= "FROM Articles ";
   $sql .= "WHERE category_id = ?";
   $sql .= "ORDER BY posting_date DESC ";
   $sql .= "LIMIT ?";

Printing the News

get-news.comp returns the latest news into an array. But, of course, users are interested in reading news, not looking at a Perl array. For that reason, we'll define two more top-level components: one to choose the category and number of articles we wish to read and one to display them.

Listing 7

First, we will create a component view-stories-form.html (Listing 7), which lets us choose a category and maximum number of stories to display. This component repeats the paradigm of creating a <select> list from a Perl array. It then invokes view-stories.html (Listing 8), a simple component which does nothing but iterate through the stories returned by get-news.comp, placing them in a nicely formatted page of HTML.

Listing 8

Conclusion

As you can see, the amount of effort and code necessary to create this site was fairly modest. And while this is a relatively simple site, it does work—and it represents one way in which Mason and databases can be used together to create a dynamic site in a minimum amount of time. True, we ended up writing a number of components; but at least two of them are reusable if we decide to expand the site in the future, and thus will reduce the amount of work and debugging we'll have to do at that time.

With a bit more work, we could add personalization to this site, allowing users to read only news that is new to them and in only the categories that interest them.

As I indicated last month, Mason has increasingly become my tool of choice for producing these sorts of web sites, because of the speed and ease with which I can do so. The fact that I can separate tasks into reusable components and the high-speed gains from working within mod_perl are bonuses that make Mason an extremely attractive web-development environment.

4066s1

email: reuven@lerner.co.il

Reuven M. Lerner , an Internet and Web consultant, moved to Modi'in, Israel following his November marriage to Shira Friedman-Lerner. He has written a book, Core Perl published by Prentice-Hall. Reuven can be reached at reuven@lerner.co.il. The ATF home page, including archives, source code and discussion forums, may be found at http://www.lerner.co.il/atf/.

______________________

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState