For my test, I have a simple XML structure:
<config> <wrapper name="testName 1.0.3" script="/usr/people/ltorvalds/apps/ +wrapper.pl" app="testApp.exe"> <envVar name="TMPDIR" value="/usr/tmp"/> <envVar name="APPDIR" value="/usr/bin"/> </wrapper> </config>
The following code successfully parses the xml file:
#!/usr/bin/perl -w use XML::Simple; my $ref = XMLin(); my $wrapName = "testName 1.0.3"; print "$ref->{wrapper}->{$wrapName}->{script}\n"; print "$ref->{wrapper}->{$wrapName}->{envVar}->{TMPDIR}->{value}\n";
... and correctly produces the following output ...
/usr/people/ltorvalds/wrapper.pl /usr/tmp
Now, if I try to treat the <envVar> tags as an array, I run into trouble:
print "$ref->{wrapper}->{$wrapName}->{envVar}->[0]->{value}\n";
... produces a blank line. I assumed that this was because I needed to turn forceArray on, so I changed the call to XMLin():
my $ref = XMLin(xmlTest.xml, forceArray=>['envVar']);
... but still I get the same blank line as output.

I suspect that I'm either using forceArray incorrectly or I'm using an invalid array reference. Any insight as to where my folly lies would be appreciated.

Update

Thanks for all of the input. What have I learned? I have learned that I need to:

  1. learn how to better use the debugger
  2. read the XML::Simple docs more carefully
  3. study up on hashes and arrays
It all pretty much boils down to rtfm, but I am grateful to all of you for the focused answers.

In reply to How do I correctly use arrays with XML::Simple? by ccarden

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.