Sanity is returning. I assigned the $configdata to "my $name" and used that in the correct places, and all is right with the world again.

Many thanks for that... I was slowly going mad. Nice to see that I was at least right about what I thought was going on, even if I had no idea why :)

Steve

Jeremy White wrote:

Its like "$configdata(name}" is being expanded/substituted when I actually press the button (using the last $configdata{name} value from the button creation stage), rather than the parameter being expanded/substituted and stored when i create the button. If I undef the %configdata, nothing is being returned. If I dont undef it, I always get the ID of the last button I created. Infuriating, to say the least.


I think you've answered your own question:) Yes, the button isn't using the value when it's first created - the whole process is dynamic - when you press the button $configdata{name} is revaluated which will returns the last value.

If you want to "capture" a value, use a closure - something like (not tested):

sub CreateButton {
 my ($win,$name)[EMAIL PROTECTED];
 my $button=$win->AddButton(
   -name => $name,
   -onClick => sub { button_click($name) },
)  ;
 return $button;
}

In this example $name becomes a closure, which will retain it's value.

An alternative is to actually associate data to the button - something like (not tested):

 my $button=$win->AddButton(
   -name => $name,
   -onClick => \&button_click,
 ) ;
 $button->UserData($somedata);

sub button_click {
 my $self=shift;
 my $somedata=$self->UserData();
}

Cheers,

jez.



Reply via email to