O'Reilly logo

Perl for Web Site Management by John Callender

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Automatically Generating Links

So, we now have covered the first part of the script, where we traverse the site and read the contents of all its pages into %page_data . Next, we’re going to traverse that %page_data data structure itself and write those pages back out to the site, replacing the old versions of the pages with new ones. We’re doing that for two reasons. First, we want to update those pages based on the latest (possibly updated) version of our template. Second, we want to add a section to each page, just after the “content” section, that features automatically generated links pointing to suitable pages elsewhere on the site.

The details of how all that gets done don’t involve any new Perl features. But they provide a good example of the kind of work that Perl is perfectly happy to shoulder when we have a lot of HTML to manipulate.

That work begins with a foreach loop that iterates over all the keys of %page_data, storing the current key in $path:

foreach my $path (sort keys %page_data) {

The first thing that happens inside the loop is that the page is re-created from the template using the &build_page subroutine. You recall that that routine requires an argument consisting of a hash of page metainformation, and conveniently, such a hash (or rather, a reference to one) is what is stored at the top level of %page_data. So, we just use $page_data{$path} to get that reference and stick it inside %{ } to dereference it and get back the original hash. We feed that to &build_page ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required