XML::Simple doesn't know your schema ahead of time; it only knows what it reads. If there's no item element under your menu, then it doesn't know that there could be. Your options:
- Test for existence of an item element, its defined-ness, and whether it's an arrayref before trying to use it as one
- Make sure to always put at least an empty item element (not so useful if you're letting a user make their own file)
- Use a different (schema-aware) XML parser that can know ahead of time what you expect.
Update: Ah, I see I've misunderstood your question. For future reference, could you use Data::Dumper to make your explanation a lot more clear? In my words, the problem you have is that the single item element in your Software Menu is not being forced into an array, despite the ForceArray setting in XMLin(). I don't know the answer to
that question.
Update 2: I believe it's your setting for keyattr that's giving you trouble. If you change it to
keyattr => { menu => 'name' }, you will get an arrayref in SoftwareMenu->{item}.
Note that you'll have to do more legwork to get the item's name attribute folded, as adding it to keyattr (
item => 'name') will require that you either disable
XML::Simple's STRICT MODE, or ensure every item has a name attribute:
Note: "XMLin()" will generate a warning (or a fatal error in "STRICT
MODE") if this syntax is used and an element which does not have the
specified key attribute is encountered (eg: a 'package' element without
an 'id' attribute, to use the example above). Warnings will only be
generated if -w is in force.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.