Showing the Details of Each Visit

We’re almost ready to test the fully functional version of the script. At the end of the report will be a “detail” section that describes each visit to the site, including the list of pages that were requested during that visit. As you might expect, this can make the report quite long (especially for popular sites), so the first thing to do in adding this feature is to put a configuration variable at the top of the script that lets us turn this feature off:

my $show_detail      = 1;   # (0 or 1) show detail?

Now, toward the end of the script proper, just before the part that prints out the $report variable, we’ll add the following:

if ($show_detail) {
    $report .= <<EndOfText;

Detail for most recent reporting period:

EndOfText

    foreach my $visit_num (1 .. $total_visits) {    
        $report .= &visit_detail($visit_num);
    }
}

As you can see, we’re going to be using a separate subroutine called &visit_detail to return the formatted details for each visit. One interesting thing here is the use of the range operator (..) to produce the list of visit numbers to feed to that subroutine. Just as we were able to use ('a' .. 'b') to produce a list of all the lowercase letters of the alphabet in Chapter 6, we can use (1 .. $total_visits) to produce a list of all the integers between 1 and $total_visits.

To format the visit details so that they look nice, with the list of visited pages being given a hanging indent, we’re going to take advantage of the standard Perl module Text::Wrap ...

Get Perl for Web Site Management now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.