KarateCowboy has asked for the wisdom of the Perl Monks concerning the following question:

Howdy howdy, I like XML Twig. However in the OO perl class I am programming I have a twig and am using to parse and XML file. So I assign the handler in the twig declaration, and when I start coding the handler in this class of mine I think "Oh, wait, the handler actually belongs to the twig, which is why the first argument is the twig itself. So, there is no way I can use this handler to take values from the XML item and pass it to an array in the class for use later on!" Am I wrong? Is there a way the class using the XML::Twig can swap info with the handlers?
package SampleClass; sub new{ my $class = shift; my $self = {}; $self->{configFile} = shift; $self->{fileContents} = (); $self->{twig} = new XML::Twig(twig_handlers => { tag => \&tagHandler, }); bless $self, $class; } . . . sub tagHandler{ my $twig = shift; my $tag = shift; my $client_name = $tag->first_child('client')->text; my $version = $tag->first_child('version')->text; }
So I have this handler, and I want to take that text I am getting and use it in SampleClass. Not sure how.

Replies are listed 'Best First'.
Re: Accessing outside variable using XML::Twig
by Joost (Canon) on Jan 31, 2008 at 20:11 UTC
      Wow. I read that. It is really cool. I can get this to work now! I did not expect to be able to do that!
Re: Accessing outside variable using XML::Twig
by GrandFather (Saint) on Jan 31, 2008 at 19:42 UTC
Re: Accessing outside variable using XML::Twig
by mirod (Canon) on Jan 31, 2008 at 20:51 UTC

    I guess this has become a FAQ, that's the second time this question is asked this week. So I added it to the XML::Twig FAQ.