Perl Rob wrote:
[slightly edited]
foreach ( @button_names ) {
my ($name) = $_;
$main->AddButton(
-name => $name,
-text => $name,
-onClick => sub { Button_Clicked ( $name ) },
...
);
}
sub Button_Clicked {
print "Your name is " . $_[0];
}
So, what you're really like is:
foreach (@button_names) {
$main->AddButton(
-name => $_,
-text => $_,
-onClick => \&clicked,
...
);
# NEM callback
sub clicked {
my ($self) = @_;
print $self->GetName(), "\n";
return 1;
}
But there is no GetName() method for the button object (or indeed any
Win32::GUI object). Slightly naughty, and I shouldn't be encouraging
this [1], but the object's name is stored in it's '-name' hash slot, so
you can replace:
print $self->GetName(), "\n";
with
print $self->{-name}, "\n";
And get what you want without the closures.
But why do you need the name, given that the NEM callback gives you the
object reference as first parameter?
Regards,
Rob.
[1] Accessing an object's internal state by any mechanism other than an
accessor method should be strongly discouraged, as it breaks the OO
encapsulation.