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

I have come across the need to use an event driven setup called from a class method. The problem is that, when I call the class method and setup the callback, I then loose all access to the class object in the callback method. Is there any way to retain visibility to the calling classes object in the callback method?

Here is a simple example:

package XMLParse; use strict; use XML::Parser; sub new { my $class = shift; my $self = { 'i_need_this' => 'important', }; bless $self, $class; return $self; } sub parse { my ($self, $xmlfile) = @_; my $parser = XML::Parser->new( Handlers => {Start => \&handle_start, End => \&handle_end, Char => \&handle_char} ); $parser->parsefile($xmlfile); } sub handle_start { .... } sub handle_end { .... } sub handle_char { .... } 1;
In this example, I need access to the attribute 'i_need_this' in the XMLParse class object from within the xml parser callback methods. How to I go about getting at it?

Replies are listed 'Best First'.
Re: Access parent class object in callback
by Corion (Patriarch) on Mar 08, 2008 at 12:25 UTC

    I think using the word "parent" is misleading in your title, because you seem to (properly) use containment/delegation instead of inheritance. If I misunderstand your setup, the following likely is also wrong:

    I would pass closures as the callbacks that contain and pass on the reference to $self as well:

    sub parse { my ($self, $xmlfile) = @_; my $parser = XML::Parser->new( Handlers => {Start => sub { $self->handle_start(@_) }, End => sub { $self->handle_end(@_) }, Char => sub { $self->handle_char(@_) }, ); $parser->parsefile($xmlfile); } sub handle_start { my ($self, $parser, @args) = @_; print $self->{i_need_this}; ... }; ...
      Yes you are correct, using 'parent' was misleading due to the absence of inheritance. Thank you for the example, however, I am somewhat confused as to why you would call $self->handle_start() and also pass @_ as well? Calling $self-> will pass the class object I am looking for, but then passing @_ would pass the class object again along with the xml file name. Am I reading this wrong?

        Yes, you are reading this a bit wrong, but that's not uncommon - I struggled with this for some time too before I got it.

        The @_ passed along will be the @_ at the time of call to the callback. Let's look at this in a simpler setting:

        use strict; use Data::Dumper; sub parse { my ($self, $xmlfile) = @_; my $handler = sub { $self->handle_start(@_) }; # time passes $handler->('foo','bar'); } sub handle_start { print Dumper \@_; }; sub new { bless {} }; my $self = main::->new(); $self->parse('some.xml');

        This outputs

        $VAR1 = [ bless( {}, 'main' ), 'foo', 'bar' ];

        As you see, the arguments to $handler are foo and bar, and the callback adds $self in front of those. The @_ in the anonymous subroutine does refer to the @_ at the moment of the call to $handler (in fact, there only ever is one @_, and it is always the one for the current call to the current subroutine). I hope that clears it up somewhat.