Obviously you can do this:

push @{ $data->{Things}->{Thing} }, { 'Item' => $var1, 'Number' => $var2, 'Details' => { 'Detail' => { 'Color' => $var3 } }, 'AdditionalInfo' => { 'Info' => $var4, 'Stuff' => $var5, 'MoreDetails' => { 'ExtraColor' => $var6 } } };

But probably it is not what you really want. If the real question is "How can I dinamically build the structure that will generate my XML?", the answer is not so simple (aka, I can't make it for you :)):

The right path to enlightment is to understand how complex data structures work in perl (your example id a HoHoHoHoH, not the very simplest) so you can handle them in a better way

As an example, the following code probably do what you expect:

$struc={ 'Item' => $var1, 'Number' => $var2, 'Details' => { 'Detail' => { 'Color' => $var3 } } } $struc->{AdditionalInfo}={ 'Info' => $var4, 'Stuff' => $var5, 'MoreDetails' => { 'ExtraColor' => $var6 } }; push @{ $data->{Things}->{Thing} },$struc;

Not sure, I can't try it now

Rule One: Do not act incautiously when confronting a little bald wrinkly smiling man.


In reply to Re: XML::Simple and Adding Additional Nested Elements by psini
in thread XML::Simple and Adding Nested Elements by Ozeroc

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.