Generating the Report

Now that the data is extracted from our log file and stored in an appropriate data structure, we’re ready for the fun part: generating useful information from all that data.

Generating the Summary Line

We start by adding a new configuration variable, up at the top of the script, to give the name of our web site as we want it to appear in the script:

my $site_name        = 'My Web Site';

Now we add the following to the middle of the script, after the main while loop is finished but before the subroutine definitions:

# done processing log file. begin output.

my $report = <<EndOfText;
$site_name Access Report -- $end_time

        From                  To           Hits   Views Visits   Mb
==================== ==================== ====== ====== ====== ======
EndOfText

This is just printing out a suitable header for the summary line, which comes next:

my $summary_line = sprintf 
    "%s %s %6u %6u %6u %6u\n", $begin_time, $end_time,
    $total_hits, $total_views, $total_visits, $total_mb;

$report .= $summary_line;

print $report;

This uses sprintf to print out a nicely formatted line summarizing the totals we kept track of during the while loop’s processing of the log file lines. The format string starts off with the formatting code %s (twice). This just formats its argument as a string; in this case, it drops in $begin_time and $end_time , which will always be 20 characters wide due to the standardized format of the log file’s date/time strings.

Tip

You might be wondering why we even need a formatting code for the $begin_time ...

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.