in reply to Access parent class object in callback

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}; ... }; ...

Replies are listed 'Best First'.
Re^2: Access parent class object in callback
by Elijah (Hermit) on Mar 08, 2008 at 12:32 UTC
    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.