Re: local variable and event handler problem
- From: "muppet" <scott asofyet org>
- To: gtk-perl-list gnome org
- Subject: Re: local variable and event handler problem
- Date: Fri, 31 Oct 2003 09:44:31 -0500 (EST)
Jens Wilke said:
i've got a problem with a button connected signal.
local defined variables aren't available in the event called subroutine.
Calling it directly works fine, but clicking the button results in an "Use of
uninitialized value in concatenation" error. ($image and $file are
uninitialized)
How can i make the locally defined variables available for the event handler?
Rgds, Jens
local $image = Gtk2::Image->new();
local $file;
my $bbutton = Gtk2::Button->new_from_stock ('gtk-go-forward');
$bbutton->signal_connect (clicked => \&loadimage, 1); # doesn't work
&loadimage(0); # this works fine
sub loadimage {
...
$image->set_from_file($file);
}
by using 'local', $image and $file have their local values only for the
duration of the block in which you connect the signal. thus, they are no
longer set when the attached sub actually runs.
'local $image' actually gives a temporary value to a global variable, which is
not what you're wanting to do.
you need to use my instead of local, and either use a closure or pass the vars
to the callback.
my ($image, $file);
...
$object->signal_connect (name => sub {
# $image and $file are available
# here, trapped in the closure's scope
});
$object->signal_connect (name => \&callback, [ $image, $file ]);
sub callback {
my ($instance, ..., $user_data) = @_;
my ($image, $file) = @$user_data;
}
--
muppet <scott at asofyet dot org>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]