Now that we have these generic stats on our system, we should replace the hard-coded numbers we have on the front page. As we know these numbers will have to be displayed on every page, we don't add it to the code that displays only the main page. Instead we add our code to the template() function that will be called for every page.

Earlier we had this code generating the HTML page from the template and from the lack of (!) parameters passed to it. That empty hash in the middle is where we need to pass the parameters.

$tt->process( "$", {}, \$out )

so instead of that we write the following code:

    my $root = root();
    my $totals = from_json path("$root/totals.json")->slurp_utf8;
    $tt->process( "$", {totals => $totals}, \$out )

We also need to change the templates. We remove the hard-coded cpanstats numbers from the tt/incl/ and instead of that we add Template::Toolkit tags. Actually, instead of putting those in the header file, I went ahead and put the tags in the tt/incl/ as they will be at the bottom of other pages as well.

This is the HTML code with the Template::Toolkit (TT) tags:

<div class="footer"><div class="cpanstats">
<% totals.distribution %> Distributions, <% totals.module %> Modules, <% %> Authors

The data we passed looked like this:

  totals => {
     distribution => 36252,
     module      => 15533974,
     author      => 11829,

TT allows us to refer to keys of a hash and if there are internal hash-es we can also access the values of those keys by separating the levels with dots. So What in perl we would write $data->{totals}{distribution}, in Template::Toolkit we write totals.distribution. That's how the above code was reached.

The only missing thing for this to work was to load the necessary modules in the lib/MetaCPAN/

use JSON qw(from_json);
use Path::Tiny qw(path);


This leads us to the next commit.

$ git add .
$ git commit -m "load cpanstats from the totals.json file"