Pretty much what Ikegami said on SO. You can see the order that the twigs and residual tags are being processed by modfying the code as follows:

use XML::Twig; use IO::Tee; use feature 'say'; open my $frufile, '>', 'fruit.xml' or die "fruit $!"; open my $vegfile, '>', 'veg.xml' or die "veg $!"; my $tee = IO::Tee->new($frufile, $vegfile); select $tee; my $twig=XML::Twig->new( twig_handlers => { thing => \&magic, _default_ => sub { print '_default_ for '.$_->name." [[["; $_[0]->flush($tee); #default filehandle = tee say "]]]"; 1; }, }, pretty_print => 'none', empty_tags => 'normal', ); $twig->parse( *DATA ); sub magic { my ($thing, $element) = @_; print "magic for ". $element->{att}{type}." [[["; for ($element->{att}{type}) { if (/fruit/) { $thing->flush($frufile); } elsif (/vegetable/) { $thing->flush($vegfile); } else { $thing->purge; } } say "]]]"; 1; } __DATA__ <batch> <header> <foo>1</foo> <bar>2</bar> <baz>3</baz> </header> <thing_list> <thing type="fruit" >Im an apple!</thing> <thing type="city" >Toronto</thing> <thing type="vegetable" >Im a carrot!</thing> <thing type="city" >Melrose</thing> <thing type="vegetable" >Im a potato!</thing> <thing type="fruit" >Im a pear!</thing> <thing type="vegetable" >Im a pickle!</thing> <thing type="city" >Patna</thing> <thing type="fruit" >Im a banana!</thing> <thing type="vegetable" >Im an eggplant!</thing> <thing type="city" >Taumatawhakatangihangakoauauotamateaturipuk +akapikimaungahoronukupokaiwhenuakitanatahu</thing> </thing_list> <trailer> <chrzaszcz>A</chrzaszcz> <zdzblo>B</zdzblo> </trailer> </batch>

Fruit.xml:

_default_ for foo[[[<batch><header><foo>1</foo>]]] _default_ for bar[[[<bar>2</bar>]]] _default_ for baz[[[<baz>3</baz>]]] _default_ for header[[[</header>]]] magic for fruit [[[<thing_list><thing type="fruit">Im an apple!</thing +>]]] magic for city [[[]]] magic for vegetable [[[]]] magic for city [[[]]] magic for vegetable [[[]]] magic for fruit [[[<thing type="fruit">Im a pear!</thing>]]] magic for vegetable [[[]]] magic for city [[[]]] magic for fruit [[[<thing type="fruit">Im a banana!</thing>]]] magic for vegetable [[[]]] magic for city [[[]]] _default_ for thing_list[[[</thing_list>]]] _default_ for chrzaszcz[[[<trailer><chrzaszcz>A</chrzaszcz>]]] _default_ for zdzblo[[[<zdzblo>B</zdzblo>]]] _default_ for trailer[[[</trailer>]]] _default_ for batch[[[</batch>]]]

Veg.xml

_default_ for foo[[[<batch><header><foo>1</foo>]]] _default_ for bar[[[<bar>2</bar>]]] _default_ for baz[[[<baz>3</baz>]]] _default_ for header[[[</header>]]] magic for fruit [[[]]] magic for city [[[]]] magic for vegetable [[[<thing type="vegetable">Im a carrot!</thing>]]] magic for city [[[]]] magic for vegetable [[[<thing type="vegetable">Im a potato!</thing>]]] magic for fruit [[[]]] magic for vegetable [[[<thing type="vegetable">Im a pickle!</thing>]]] magic for city [[[]]] magic for fruit [[[]]] magic for vegetable [[[<thing type="vegetable">Im an eggplant!</thing> +]]] magic for city [[[]]] _default_ for thing_list[[[</thing_list>]]] _default_ for chrzaszcz[[[<trailer><chrzaszcz>A</chrzaszcz>]]] _default_ for zdzblo[[[<zdzblo>B</zdzblo>]]] _default_ for trailer[[[</trailer>]]] _default_ for batch[[[</batch>]]]

Notice how the "<thing>" handler gets called after the tag is closed and flushes any other previous, unprocessed tag information before it.


In reply to Re^3: Buggy output from XML::Twig on a Tee by ateague
in thread Buggy output from XML::Twig on a Tee by seki

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.