Syntactic Confectionery Delight | |
PerlMonks |
Behaviour of parsed XMLby dalgetty (Acolyte) |
on Feb 26, 2020 at 11:08 UTC ( [id://11113429]=perlquestion: print w/replies, xml ) | Need Help?? |
dalgetty has asked for the wisdom of the Perl Monks concerning the following question: 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:The data is then correctly constructed: 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:
"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:
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
Back to
Seekers of Perl Wisdom
|
|