Displaying Tags In Perl Using HTML::TagCloud

CPAN contains a module called HTML::TagCloud that displays tag clouds. It is faster (but not as flexible) to use this module than to write your own code to display tag clouds. We'll develop our own code for displaying tag clouds in a moment, but first let's take a look at the easier method. Here is a sample script that uses HTML::TagCloud:

#!/usr/bin/perl

use HTML::TagCloud;

use strict;
use warnings;

require "genesis.pl";

my $cloud = HTML::TagCloud->new;

foreach my $tag (keys %{$tags})
{

    $cloud->add($tag, $tags->{$tag}->{url}, $tags->{$tag}->{count});
}

print $cloud->html_and_css();

This code produces a tag cloud with centered words using font sizes of 12 to 36 points. If you wish to customize the look of the tag clouds produced by this method, you'll need to modify the CSS code. You can do this by providing a custom CSS file and using this alternate function to produce the tag cloud:

print $cloud->html();

Get Building Tag Clouds in Perl and PHP 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.