Dear Brethren,

I know that Perl is not inconsistent, so it must be me.

For years I have been using XML::Simple to parse several RSS feeds, and have 1500 lines of code running nicely, except for the rare occasion when there is only one item in the RSS feed.

In this case the script fails, because $data->{channel}->{item}->[0] does not exist. Since there is only one entry, XML::Simple does not create {item}->[0], but puts the hash table straight into $data->{channel}.

So I adjust the hash table as follows, and I can access the information I need:
$mydata=$data->{channel}->{item}; my $data->{channel}->{item}->[0]=$mydata; if ($data->{channel}->{item}->[$y]) { while (($data->{channel}->{item}->[$y])&&($y>-1)) { $keyword=$data->{channel}->{item}->[$y]->{epfl_keywords}; ...
The data is then correctly constructed:
$VAR1 = { 'channel' => { 'item' => [ { 'epfl_is_internal' => 'False', +'link' => 'http://memento.epfl.ch/event/an-account-of-the-world-s-fir +st-cubesat/', 'epfl_organizer' => 'eSpace ', 'pubDate' => 'Mon, 16 Ma +r 2020 14:00:00 +0100', 'description' => "Incl ...
However, this code needs to run when there are several items in the RSS feed too, so I only want to apply the above operation in cases of one item. In order to test for this I use the following code:
if (exists($data->{channel}->{title})) { $mydata=$data->{channel}->{item}; my $data->{channel}->{item}->[0]=$mydata; print "Only one item is present"; } if ($data->{channel}->{item}->[$y]) { while (($data->{channel}->{item}->[$y])&&($y>-1)) { $keyword=$data->{channel}->{item}->[$y]->{epfl_keywords}; ...

"title" is one of many keys that always exists in the RSS feed entries. If it exists directly within "channel" that means that there is only one RSS entry in the feed, and the message prints out "Only one item is present". So far, so good.

However, I then get an error: "Not an ARRAY reference" for the second "if" statement in line 6, as if the restructuring had not happened.

This seemed strange to me, since the single entry case had clearly been identified correctly. So I tried the following:

if (1) { $mydata=$data->{channel}->{item}; my $data->{channel}->{item}->[0]=$mydata; } if ($data->{channel}->{item}->[$y]) { while (($data->{channel}->{item}->[$y])&&($y>-1)) { $keyword=$data->{channel}->{item}->[$y]->{epfl_keywords}; ...

I fully expected this code to run smoothly, like the first attempt did. Of course, I would never have attempted to make Perl look inconsistent. But I get the "not an array" error again.

All I am doing is testing for something, not changing anything. But just making an if(1) statement is enough to stop my code working correctly. What is even more confusing is that a Dumper print of the data shows that the data is correctly structured, as in the first statement, whether I apply the if statement or not.

Can any of you please tell me where my inconsistency lies? Thanks to all


In reply to Behaviour of parsed XML by dalgetty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.