http://qs1969.pair.com?node_id=656995

absolut.todd has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, a friend of mine has been struggling with an aspect of XML::Twig. He wants to be able to call private methods on his object from within the handlers. However the problem is that the handler params are the $twig and the element.

The solution i suggested is as follows, where the create_sub method is called which then creates a reference to $self before it is assigned to the handler.

Is this the correct way to do this? Or can someone suggest a better way.

#!/usr/bin/perl -w use warnings; use strict; package Stupid; { use XML::Twig; sub new { my ( $caller, $args ) = @_; my $class = ref($caller) || $caller; my $self = {}; $self->{a_note} = "This should print something\n"; bless $self, $class; my $code_ref = $self->create_sub; my $t = XML::Twig->new( twig_handlers => { 'a/b' => $code_ref +} ); my $xml = "<a><b>3</b><c>4</c></a>"; $t->parse($xml); $t->flush; } sub create_sub { my $hidden_self = shift; return sub { my ( $self, $t ) = @_; print ref($self) . "\n"; print ref($t) . "\n"; print $t->text; print "\n\n"; $hidden_self->gah(); }; } sub gah { my $self = shift; print ref($self) . "\n"; print "$self->{a_note}\n"; } }; my $s = Stupid->new();