neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Avout.

Why does this code not return feed titles or URLs? Even the dump does not look like what I expect. UPDATE fixed, but still not sure why the dump of $e looked so strange.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; use local::lib; use XML::Feed; my $args = ({}); $args->{debug} = 1; sub atom_feed { my ( $arg ) = @_; # Set defaults # If option given my $newer_than = exists $arg->{newer_than} ? $arg->{newer_than} : 6000; my $url = $arg->{url}; my @events; warn "\n>>>> Getting atom url for [$url] ". "records newer than [$newer_than]min" if $args->{debug}; my $feed = XML::Feed->parse( URI->new( $url )) or die "Feed error with [$url] ".XML::Feed->errstr; my $x=1; for my $e ( $feed->entries ) { last if $x == 3; warn Dumper( $e ) if $args->{debug}; # << looks weird warn "Got bug title [$e->title]" if $args->{debug}; # << nothing if ( $e->title =~ m/\A\w+ # Start with any word \s+ # UPDATE << forgot this! \#\d{4,5} # bug number \s+ \( (Open|Closed|Merged|Rejected) \) # Status of bug /ix ) { push @events, $e->title .", ". $e->link; } $x++; } say $_ foreach ( @events ); # empty !? return \@events; } atom_feed({ url => 'https://dev.cfengine.com/projects/core/activity.atom' }); atom_feed({ url => 'http://www.redmine.org/projects/redmine/activity.atom' }); $ ./atom.pl Warning: XML::LibXML compiled against libxml2 20902, but runtime libxm +l2 is older 20901 >>>> Getting atom url for [https://dev.cfengine.com/projects/core/activity.atom] records newe +r than [6000]min at ./atom.pl line 22. $VAR1 = bless( { 'entry' => bless( { 'version' => '0.3', 'elem' => bless( do{\(my $o = 68470752)}, 'XML::LibXML::Element +' ), 'ns' => 'http://www.w3.org/2005/Atom' }, 'XML::Atom::Entry' ) }, 'XML::Feed::Entry::Format::Atom' ); Got bug title [XML::Feed::Entry::Format::Atom=HASH(0x43b0968)->title] +at ./atom.pl line 33. $VAR1 = bless( { 'entry' => bless( { 'ns' => 'http://www.w3.org/2005/Atom', 'elem' => bless( do{\(my $o = 68348704)}, 'XML::LibXML::Element +' ), 'version' => '0.3' }, 'XML::Atom::Entry' ) }, 'XML::Feed::Entry::Format::Atom' ); Got bug title [XML::Feed::Entry::Format::Atom=HASH(0x3ac4918)->title] +at ./atom.pl line 33. >>>> Getting atom url for [http://www.redmine.org/projects/redmine/activity.atom] records new +er than [6000]min at ./atom.pl line 22. $VAR1 = bless( { 'entry' => bless( { 'version' => '0.3', 'elem' => bless( do{\(my $o = 68856240)}, 'XML::LibXML::Elem +ent' ), 'ns' => 'http://www.w3.org/2005/Atom' }, 'XML::Atom::Entry' ) }, 'XML::Feed::Entry::Format::Atom' ); Got bug title [XML::Feed::Entry::Format::Atom=HASH(0x3b02bd0)->title] +at ./atom.pl line 33. $VAR1 = bless( { 'entry' => bless( { 'version' => '0.3', 'ns' => 'http://www.w3.org/2005/Atom', 'elem' => bless( do{\(my $o = 67673440)}, 'XML::LibXML::Elem +ent' ) }, 'XML::Atom::Entry' ) }, 'XML::Feed::Entry::Format::Atom' ); Got bug title [XML::Feed::Entry::Format::Atom=HASH(0x3f782c8)->title] +at ./atom.pl line 33.

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: XML::Feed not working
by kcott (Archbishop) on Jul 13, 2015 at 15:07 UTC

    G'day Neil,

    "... does not look like what I expect."

    You haven't stated what your expectations are. In the remainder of my post, references to your expectations are just guesses. If they're good guesses, all's well; if not, post your expectations.

    In the code

    warn "Got bug title [$e->title]"

    you're expecting the return value of $e->title to be interpolated into the string. It doesn't work like that. Here's a quick and dirty example:

    $ perl -Mstrict -wE 'my $e = bless {} => "XXX"; say "[$e->title]"' [XXX=HASH(0x7fc0c9805480)->title]

    One way to get what you want is to wrap $e->title in @{[...]}:

    $ perl -Mstrict -wE 'my $e = bless {} => "XXX"; say "[@{[$e->title]}]" +' Can't locate object method "title" via package "XXX" at -e line 1.

    Now an attempt is made to call the object's method. Using this syntax in your warn code may provide a little more insight into what's happening:

    warn "Got bug title [@{[$e->title]}]"

    However, it appears to me that the most urgent thing you need to deal with is:

    Warning: XML::LibXML compiled against libxml2 20902, but runtime libxm +l2 is older 20901

    — Ken

Re: XML::Feed not working
by Athanasius (Archbishop) on Jul 14, 2015 at 04:23 UTC

    Hello neilwatson,

    Why does this code not return feed titles or URLs?

    If you look at the XML for one of the feeds in a browser, you’ll see that the feed title is not part of an <entry>...</entry> element. To access it, use this:

    my $feed = XML::Feed->parse(URI->new($url)) or die "Feed error with [$url] " . XML::Feed->errstr; print 'Feed title is ', $feed->title, "\n"; # ADD THIS LINE

    Output:

    14:12 >perl 1305_SoPW.pl >>>> Getting atom url for [https://dev.cfengine.com/projects/core/acti +vity.atom] records newer than [6000]min at 1305_SoPW.pl line 37. Feed title is CFEngine 3 Community Edition: Activity $VAR1 = bless( { ...

    I don’t know what URLs you’re looking for. But the XML::Feed module has an example script named check_feed.pl which you might find useful as a guide.

    BTW, in this:

    my $args = ({}); $args->{debug} = 1;

    the parentheses are unnecessary, and you can save a line by writing just:

    my $args = { debug => 1 };

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,