RSS is a bit of a mess —there are three versions, each fixing design flaws in the previous— so when I needed to generate a feed, I made it an atom feed.
I used XML::Atom::Syndication with these helpers:
sub new_atom_obj {
my $class = 'XML::Atom::Syndication::' . shift;
( my $file = $class ) =~ s{::}{/}g;
$file .= '.pm';
require($file);
unshift @_, $class;
goto &{ $class->can('new') };
}
sub set_fields {
my $self = shift;
while (@_) {
my $field = shift;
my $val = shift;
$self->$field($self, $val);
}
return $self;
}
The code looked like this:
my $atom_dt_format = DateTime::Format::Atom->new();
my $feed_title = ...;
my $feed_content_url = ...;
my $feed = set_fields( new_atom_obj('Feed'),
title => set_fields( new_atom_obj('Text', Name => 'title'), type =>
+ 'text', body => $feed_title ),
link => set_fields( new_atom_obj('Link'), href => $feed_content_ur
+l ),
);
while (...) {
my $entry_title = ...;
my $entry_content_url = ...;
my $entry_pub_dt = ...;
my $entry_body = ...;
$feed->add_entry(
set_fields( new_atom_obj('Entry'),
title => set_fields( new_atom_obj('Text', Name => 'title'
+), type => 'text', body => $entry_title ),
link => set_fields( new_atom_obj('Link', Name => 'link')
+, href => $entry_content_url ),
published => $atom_dt_format->format_datetime($entry_pub_dt),
content => set_fields( new_atom_obj('Content'), type => 'ht
+ml', body => $entry_body),
),
);
}
$feed->updated($atom_dt_format->format_datetime($feed_last_updated));
return $feed->as_xml();
|