in reply to Re: Access parent class object in callback
in thread Access parent class object in callback

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?
  • Comment on Re^2: Access parent class object in callback

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

    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.