This is a remarkably simple trick which I’ve found very handy. With a few lines of Perl you can take any RSS feed and format it to your liking.

Get the Feed

You can do this using LWP::Simple:

Perl
1
2
3
4
5
use LWP::Simple;
 
my $feed_url = 'http://feeds.bbci.co.uk/news/rss.xml';
my $feed = get($feed_url)
        or die ("Failed to fetch feed.");
use LWP::Simple;

my $feed_url = 'http://feeds.bbci.co.uk/news/rss.xml';
my $feed = get($feed_url)
        or die ("Failed to fetch feed.");

Process the Raw Result

Using XML::RSS, convert the raw feed into a more manageable hash.

Perl
1
2
3
4
use XML::RSS;
 
my $rss = XML::RSS->new();
$rss->parse($feed);
use XML::RSS;

my $rss = XML::RSS->new();
$rss->parse($feed);

Format to Your Liking

Template::Toolkit can take in a template and a hash reference of values to substitute into the template.

Perl
1
2
3
4
5
6
7
8
9
# Define a template
my $template = <<"TEMPLATE";
[% channel.title %]
 
Headlines:
[% FOREACH item = items %]
[% item.pubDate %]t[% item.title %]
[% END %]
TEMPLATE
# Define a template
my $template = <<"TEMPLATE";
[% channel.title %]

Headlines:
[% FOREACH item = items %]
[% item.pubDate %]t[% item.title %]
[% END %]
TEMPLATE

This simple template will take the BBC news feed from above and print out a list of headlines with publication dates.

Perl
1
2
3
4
5
6
my $tt = Template->new()
        or die ("Failed to load template: $Template::ERRORn");
 
# Combine the template with the processed RSS feed.
$tt->process ( $template, $rss )
        or die $tt->error();
my $tt = Template->new()
        or die ("Failed to load template: $Template::ERRORn");

# Combine the template with the processed RSS feed.
$tt->process ( $template, $rss )
        or die $tt->error();

Putting it All Together

Perl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/perl
use strict;
use warnings;
 
use XML::RSS;
use LWP::Simple;
use Template;
 
##################
# Configuration:
#
##################
my $feed_url = 'http://feeds.bbci.co.uk/news/rss.xml';
my $template = <<"TEMPLATE";
[% channel.title %]
 
Headlines:
[% FOREACH item = items %]
[% item.pubDate %]t[% item.title %]
[% END %]
TEMPLATE
 
##################
##################
 
my $tt = Template->new()
        or die ("Failed to load template: $Template::ERRORn");
my $feed = get($feed_url)
        or die ('Failed to fetch feed.');
my $rss = XML::RSS->new();
$rss->parse($feed);
 
$tt->process ( $template, $rss )
        or die $tt->error();
#!/usr/bin/perl
use strict;
use warnings;

use XML::RSS;
use LWP::Simple;
use Template;

##################
# Configuration:
#
##################
my $feed_url = 'http://feeds.bbci.co.uk/news/rss.xml';
my $template = <<"TEMPLATE";
[% channel.title %]

Headlines:
[% FOREACH item = items %]
[% item.pubDate %]t[% item.title %]
[% END %]
TEMPLATE

##################
##################

my $tt = Template->new()
        or die ("Failed to load template: $Template::ERRORn");
my $feed = get($feed_url)
        or die ('Failed to fetch feed.');
my $rss = XML::RSS->new();
$rss->parse($feed);

$tt->process ( $template, $rss )
        or die $tt->error();
Bash
1
2
3
4
5
6
7
8
9
10
11
12
rob@arrakis:~/public_html/rss-reader$ perl rss-reader.pl 
BBC News - Home
 
Headlines:
 
Fri, 14 Sep 2012 12:15:22 GMT   Kate privacy invasion 'grotesque'
 
Fri, 14 Sep 2012 11:49:10 GMT   US missions on film protest alert
 
Fri, 14 Sep 2012 12:25:25 GMT   Alps attack girl returning to UK
 
Fri, 14 Sep 2012 10:17:03 GMT   Woman is held after car body find
rob@arrakis:~/public_html/rss-reader$ perl rss-reader.pl 
BBC News - Home

Headlines:

Fri, 14 Sep 2012 12:15:22 GMT	Kate privacy invasion 'grotesque'

Fri, 14 Sep 2012 11:49:10 GMT	US missions on film protest alert

Fri, 14 Sep 2012 12:25:25 GMT	Alps attack girl returning to UK

Fri, 14 Sep 2012 10:17:03 GMT	Woman is held after car body find

In this example the RSS feed and template are defined in code but they can just as easily be defined in files or a database allowing for changes/additions without deploying new code.