Hello,
I'm a pretty new to both Perl, and the Win32::GUI module. If this isn't
the right place to ask this question, let me know, and I'll go away.
In my program, there's several times I come up with a list of items I want
the user to choose from. So I wrote a subroutine to display a small dialog
box asking the user to select one item from a given list:
my $chooseWin;
...
sub select_from_list {
my @list = @_;
my $item;
$chooseWin = new Win32::GUI::DialogBox( ... );
$chooseWin->AddListbox( -name => 'chooseList', ... );
$chooseWin->AddButton( -name => 'chooseOK', ... );
$chooseWin->chooseList->AddString($_) for (@list);
$chooseWin->Show();
Win32::GUI::Dialog();
$chooseWin->Hide();
return $item;
sub chooseOK_Click {
$item = $chooseWin->chooseList->SelectedItem();
return -1;
}
}
This works the first time I call &select_from_list. Every time after
that, it always returns undef. After hunting through documentation, I
gather this happens because chooseOK_Click is compiled only once (in this
case, the first time it's called), and so only has access to the variables
visible to it when it's compiled. Since $item is lexical to
select_from_list, the second time I call chooseOK_Click, it's $item is a
reference to the same $item from the first time I called select_from_list,
which is not the $item I'm using in my second call to select_from_list.
Do I understand this right?
Because I want select_from_list to return whatever value they select that
time, I declare $item global to the program. This fixes my problem, but I
don't know if it's the right way to go about this - I think I want
select_from_list to be entirely self-contained. But I can't do this as
long as the event subroutines are compiled only once, even when inside
other subroutines. Am I doing this the wrong way?
--
-Matt Stegman
|