in reply to Re^2: Access parent class object in callback
in thread Access parent class object in callback
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.
|
|---|