2

Stackoverflow's feeds are Atom feeds and I was not able to parse them using XML::RSS itself and have tried other parsers that are currently developed but still unsuccessful. The closest that I have got to parsing out the feeds was with the XML::Atom::Feed module, but can not wrap my brain around dereferencing the link objects of the entries.

use strict;
use warnings;
use feature 'say';
use XML::Atom::Feed;

my $url  = 'http://stackoverflow.com/feeds/tag?tagnames=r&sort=newest';

my $feed = XML::Atom::Feed->new(URI->new($url)) 
                or die XML::Atom::Feed->errstr;

for ($feed->entries) {
  say $_->title, "\n", $_->link;
}

Which outputs the latest thirty entries as follows:

Rcpp: Returning C array as NumericMatrix to R
XML::Atom::Link=HASH(0x24cbf00)

I was aware that the link object was a hash reference but I seem to be missing something.

Also, is there another module better for parsing XML Atom feeds?

hwnd
  • 69,796
  • 4
  • 95
  • 132

3 Answers3

3

XML::Atom is not very thoroughly documented. It's quite a good collection of modules once you figure it out though. Anyway, links are blessed objects with various methods available, including href.

Try something like:

for ($feed->entries) {
  say $_->title, "\n", $_->link->href;
}

The source code for XML::Atom::Link will show you the other methods available. It's mostly pretty easy to follow.

tobyink
  • 13,478
  • 1
  • 23
  • 35
  • I accepted your answer since you first provided the correct implementation of the `href()` method and the source. – hwnd Oct 04 '14 at 16:48
2

As XML::Atom::Feed documentation states, link() method is context sensitive, so depending on what you need you might want to use scalar context, either by forcing by scalar or implicit scalar context when assigning to $link,

for ($feed->entries) {
  my $link = $_->link;
  say $_->title, "\n", $link->href;
}
mpapec
  • 50,217
  • 8
  • 67
  • 127
  • Yes now I do, I couldn't find any documentation for it before. Thanks. – hwnd Oct 04 '14 at 16:46
  • `$_` is an [XML::Atom::Entry](https://metacpan.org/pod/XML::Atom::Entry), not an [XML::Atom::Feed](https://metacpan.org/pod/XML::Atom::Feed). (Though as it happens, the behaviour of the `link()` method is identical in both, as they inherit it from an abstract base class.) – tobyink Oct 04 '14 at 17:29
1

As the documentation says, the link() method returns an object of type XML::Atom::Link. Documentation for that class is non-existent, but it seems that it has an href() method which gives the result that you want.

for ($feed->entries) {
  say $_->title, "\n", $_->link->href;
}
Dave Cross
  • 68,119
  • 3
  • 51
  • 97