How you ever got that code to even compile with this line in it I don't get (quotes are missing and the semicolon should be a comma)

*[@att='type'] => \&changeTagAttrOrderH +andler;

But anyway, your problem might be that this line

$rootElt->set_atts( { $att1 => $att1Val, $att2 => $att2Val, $a +tt3 => $att3Val});
makes an ordinary hash (not an IxHash) and passes it to the set_atts method, so the order of the elements is lost.

As a simple standalone example, this code gives the attributes in a fixed order but it doesn't if you comment out the line with the arrow

use warnings; use strict; use XML::Twig; use Tie::IxHash; my $tw = XML::Twig->new("keep_atts_order", 1); my %atts; tie %atts, Tie::IxHash::; # <----- %atts = ("year", 2010, "month", 3, "day", 5); $tw->set_root(XML::Twig::Elt->new("date", \%atts)); $tw->flush; print "\n"; __END__

Alternately use the set_att method like this:

use warnings; use strict; + use XML::Twig; + my $tw = XML::Twig->new("keep_atts_order", 1); $tw->set_root(my $de = XML::Twig::Elt->new("date")); $de->set_att("year", 2010, "month", 3, "day", 5); $tw->flush; print "\n"; __END__
(But note that set_att does not delete the existing attributes, so you may need to call del_atts first.)

In reply to Re: set attributes to an element in user defined order by ambrus
in thread set attributes to an element in user defined order by bharathinc

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.