Creating an Atom feed in Perl

It seems like the people that should be positioning Atom as a format for everyday use might not be doing enough... Finding the latest specification for the Atom Syndication Format on Google is difficult: there are lots of different versions out there; AtomEnabled.org is out of date and barely mentions AtomPub.org, which still only has a text version of the specification.

The Perl modules on CPAN (XML::Atom and XML::Atom::SimpleFeed) are still stuck on Atom 0.3. Actually, I just noticed that XML::Atom::SimpleFeed has been taken over by another maintainer and now has an experimental version that produces Atom 1.0. XML::Atom also has experimental support for Atom 1.0, but it takes a bit of cleaning up to get it into a valid and non-crufty state:


$xml =~ s| xmlns="http://www.w3.org/2005/Atom"||g;
$xml =~ s| xmlns="http://purl.org/atom/ns#"||g;
$xml =~ s| xmlns:default="http://www.w3.org/1999/xhtml"||g;
$xml =~ s|<content mode="xml">|<content type="xhtml">|g;
$xml =~ s|<content mode="escaped">|<content type="html">|g;
$xml =~ s|<feed>|<feed xmlns="http://www.w3.org/2005/Atom">|;

Here's an example of using XML::Atom to produce an Atom 1.0 feed:


#!/usr/bin/perl
use strict;
use XML::Atom;
use XML::Atom::Feed;
use XML::Atom::Entry;
use XML::Atom::Person;
use DateTime;
my $dt = DateTime->now;
my $now = $dt->ymd . 'T' . $dt->hms . 'Z';
my $feed = XML::Atom::Feed->new( Version => '1.0' );
$feed->title( 'Title of the Feed' );
$feed->id('tag:www.example.com,2005:feed:feed-title');
$feed->subtitle('Description of the feed');
$feed->updated( $now );
my $self_link = XML::Atom::Link->new;
$self_link->type('application/atom+xml');
$self_link->rel('self');
$self_link->href( 'http://' . $ENV{'SERVER_NAME'} . $ENV{'SCRIPT_NAME'} );
$feed->add_link( $self_link );
my $html_link = XML::Atom::Link->new;
$html_link->type('text/html');
$html_link->rel('alternate');
$html_link->href('http://www.example.com/html_version_of_the_feed.html');
$feed->add_link( $html_link );
my $author = XML::Atom::Person->new;
$author->name('Your Name');
$author->email('you@example.com');
$feed->author( $author );
foreach my $item (@items){
    my $date = "2005-$month-$day" . "T" . "$hour-$minute-$second" . "Z";
                    
    my $entry = XML::Atom::Entry->new;
    $entry->title( $item_title );
    $entry->content( $item );
    $entry->link( $item_url );
    $entry->id( "www.example.com,2005:item:$id" );
    $entry->updated( $date );
    
    $feed->add_entry($entry);
}
print "Content-type: application/atom+xml; charset=utf-8\n\n";
$feed->print;