accessing object variables from callback function



Hi,

I'm trying to write a Class which uses XML::Parser. XML::Parser uses
callback functions. If I use an object method as the callback
function, how can I access object variables from within that callback
function if it doesn't get passed a reference to $self? Here's what I
have so far:

-----BEGIN PERL-----
package Test;

use XML::Parser;

sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = { @_ };
bless($self, $class);
return $self;
}

sub load {
my $parser = new XML::Parser(ErrorContext => 2);
$parser->setHandlers(Start => \&_start_handler);
$parser->parsefile("file.xml");
}

sub _start_handler {
# In this function, I want to set $self->{var}, however don't have
# access to $self
}

sub getVar {
my $self = shift;
return $self->{var};
}
-----END PERL-----


Thanks in advance,
-Kai

.



Relevant Pages

  • Re: accessing object variables from callback function
    ... If I use an object method as the callback ... how can I access object variables from within that callback ... Then don't use the object method, but use a closure that calls the ... sub load { ...
    (comp.lang.perl.misc)
  • Re: Emulate AddressOf operator
    ... > any of it methods as a callback routine (in other words, ... Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ... Public Sub TimerProc() ... Public Property Let Interval ...
    (microsoft.public.vb.winapi)
  • Re: ComClass + Events
    ... verwende statt Events lieber Callback. ... erstelle eine Klasse in der du die Referenz vom VB6 Objekt merkst, ... ' A creatable COM class must have a Public Sub New() ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: ComClass + Events
    ... verwende statt Events lieber Callback. ... erstelle eine Klasse in der du die Referenz vom VB6 Objekt merkst, ... ' A creatable COM class must have a Public Sub New() ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: Emulate AddressOf operator
    ... But surely the callback cannot access any class member variables, ... > Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ... > Public Sub TimerProc() ... > Public Property Let Interval ...
    (microsoft.public.vb.winapi)