Following on from a comment on a previous post, this post will demonstrate how to use XML::Feed and Template::Toolkit to format your recent Stackoverflow activity, suitable for including on your own web page. Aptly, this was aided by a stackoverflow question!

This time we can use XML::Feed both to get and parse the feed:

Perl
1
2
use XML::Feed;
my $feed = XML::Feed->parse(URI->new('http://stackoverflow.com/feeds/user/1691146'));
use XML::Feed;
my $feed = XML::Feed->parse(URI->new('http://stackoverflow.com/feeds/user/1691146'));

We need to adapt the template to account for XML::Feed’s differences:

Perl
1
2
3
4
5
6
7
8
my $template = <<"TEMPLATE";
[% feed.title %]
 
Activity:
[% FOREACH item = feed.items %]
[% item.issued.dmy %] [% item.issued.hms %]t[% item.title %]
[% END %]
TEMPLATE
my $template = <<"TEMPLATE";
[% feed.title %]
 
Activity:
[% FOREACH item = feed.items %]
[% item.issued.dmy %] [% item.issued.hms %]t[% item.title %]
[% END %]
TEMPLATE

And the template toolkit invocation:

Perl
1
2
$tt->process( $template, { 'feed' => $feed } )
  or die $tt->error();
$tt->process( $template, { 'feed' => $feed } )
  or die $tt->error();

Full example:

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
#!/usr/bin/perl
 
use strict;
use warnings;
 
use XML::Feed;
use Template;
 
my $stackoverflow_id = 1691146;
my $stackoverflow_url = "http://stackoverflow.com/feeds/user/$stackoverflow_id";
 
my $template = <<"TEMPLATE";
[% feed.title %]
 
Activity:
[% FOREACH item = feed.items %]
[% item.issued.dmy %] [% item.issued.hms %]t[% item.title %]
[% END %]
TEMPLATE
 
my $tt = Template->new( 'STRICT' => 1 )
  or die "Failed to load template: $Template::ERRORn";
 
my $feed = XML::Feed->parse(URI->new($stackoverflow_url));
 
$tt->process( $template, { 'feed' => $feed } )
  or die $tt->error();
#!/usr/bin/perl

use strict;
use warnings;

use XML::Feed;
use Template;

my $stackoverflow_id = 1691146;
my $stackoverflow_url = "http://stackoverflow.com/feeds/user/$stackoverflow_id";

my $template = <<"TEMPLATE";
[% feed.title %]
 
Activity:
[% FOREACH item = feed.items %]
[% item.issued.dmy %] [% item.issued.hms %]t[% item.title %]
[% END %]
TEMPLATE

my $tt = Template->new( 'STRICT' => 1 )
  or die "Failed to load template: $Template::ERRORn";

my $feed = XML::Feed->parse(URI->new($stackoverflow_url));

$tt->process( $template, { 'feed' => $feed } )
  or die $tt->error();

This is still a really simple script with added support for Atom feeds.