Hey, sorry for the slow response. I left for a vacation shortly after I posted my question. The filehandle is not in scope because the handlers are defined in a module and passed to the twig like this: (paraphrased)

... use Handlers::Library qw/:handlers/; ... open my $filehandle, '>', 'output.xml' or die ">output.xml:$!"; my $p = XML::Twig->new( twig_handlers => { foo => \&foo_handler }, );

I'm going to end up doing what I was trying to avoid in the first place: passing a filehandle to all of the ->flush calls. When I asked the original question, I figured this was something XML::Twig would naturally support. (It's wonderfully DWIMmy, and does something similar for defaulting a filehandle with twig_print_outside_roots => $filehandle, but I didn't realize that that was just a select.)

So, long story short, my final code will be something like: (untested)

package Handlers::Library; my $twig_fh; BEGIN { $twig_fh = *STDOUT; } sub twig_output { my $name = shift; open $twig_fh, '>', $name or die ">$name: $!"; } sub foo_handler { ... $twig->flush($twig_fh) } sub bar_handler { ... $twig->flush($twig_fh) }
use Handlers::Library qw/:handlers/; twig_output('output.xml'); my $p = XML::Twig->new(...);

In reply to Re: XML::Twig outputting root element start tag twice by benizi
in thread XML::Twig outputting root element start tag twice by benizi

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.