Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: XML::Simple Array bombs out with 1 Item in document

by derby (Abbot)
on Jun 14, 2012 at 18:52 UTC ( #976279=note: print w/replies, xml ) Need Help??

in reply to XML::Simple Array bombs out with 1 Item in document

Hmm ... well ... once you get paste the dodgy use of eval (do you really need to do that ... probably not). Basically the core problem is your data structure is not a hash of hashes but a hash of arrays of hashes.

foreach my $e ( @{$data->{channel}[0]{items}} ) { ... print $e->{title}[0] ... }
but you're probably better of just using XML::RSS:
my $rss = XML::RSS->new( version => '2.0' ); my $rss->parse( $rss_xml ); # print the title and link of each RSS item foreach my $item ( @{$rss->{items}} ) { print "title: $item->{title}\n"; print "link: $item->{link}\n"; print "desc: $item->{description}\n"; }


Replies are listed 'Best First'.
Re^2: XML::Simple Array bombs out with 1 Item in document
by Anonymous Monk on Jun 14, 2012 at 19:46 UTC
    Thanks. But my code does work well when there are 2 or more items

      That's great. But I hear the code doesn't work well when there is only 1 item.

      And that's the case that derby is talking about. You could check to see if you've got a hash (1 item) or an array (2+ items), using ref, or see about making them always be arrays in the module's options (see ForceArray).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://976279]
[choroba]: Dangers of WFH: a mailman rings, brings a parcel for my wife, I need to go outside, take my son with me, sign a paper *and* into the postman's mobile app, get back. Guess what the soup was doing meanwhiles?
[Discipulus]: soup without heat protections as CPUs are doomed to burned..
[1nickt]: The dangers of milk-based soup, you mean?
[Corion]: choroba: Ouch! But you don't need that many external events for that. I have on several occasions set up milk to cook and then programmed a bit, only to find the milk burned and congealed over the stove :-/
[choroba]: it's still edible, just need to scour the pot
[1nickt]: Corion I have a large site I need to check for broken links and absolute links. Making a scraper is easy of course; a spider that crawls a whole site is a little more involved ... I was planning a queue-based tool. Intersted to see what you do...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2017-10-18 11:41 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (244 votes). Check out past polls.