http://www.perlmonks.org?node_id=997048


in reply to Re^3: XML::Twig - provide additional parameters to twig_roots handlers
in thread XML::Twig - provide additional parameters to twig_roots handlers

Well, the XML::Twig object is an old fashion hashref, and I have no plan to change this, so you can always add your own fields to it. Or use a "hidden" attribute (one which names starts with '#') to attach private data to any element, including the root.

A closure is the cleanest way to go though. It's not a trick, it's just a common idiom.

BTW, a google search on "XML::Twig passing data to handlers" (or using "arguments" instead of data) returns plenty of answers to that question.

  • Comment on Re^4: XML::Twig - provide additional parameters to twig_roots handlers

Replies are listed 'Best First'.
Re^5: XML::Twig - provide additional parameters to twig_roots handlers
by Jenda (Abbot) on Oct 03, 2012 at 19:00 UTC

    The problem with adding own fields into the hashref is that you might unintentionally step over something. Or have an update to the module break your script, because the module starts using some key you added. That's why I specifically reserved $parser->{pad} as a place for user defined data attached to a parser and $parser->{parameters} as a place where you can find the additional parameter passed to the $parser->parse() call in XML::Rules.

    Agreed on the closure being the cleanest way.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Re^5: XML::Twig - provide additional parameters to twig_roots handlers
by Anonymous Monk on Oct 03, 2012 at 15:18 UTC

    Or use a "hidden" attribute (one which names starts with '#') to

    I take it that means twig will ignore it?

    When I stashed data I usually prefer $$t{_anony_foo} or $$t{_progname_foo} or $$t{_subclassname_foo}