3

I am trying to pass arguments to a perl subroutine which processes an wxPerl EVT_BUTTON event. I understand that when I use:

EVT_BUTTON($frame, $button, \&onClick);

I will have access to $frame and $button and its functions in onClick. However, I am trying to get the subroutine to read additional arguments, mainly the value/status of various other Wx::CheckBox values.

The only way I see this working is by using global variables and get onClick to read them, but I want to reuse onClick somewhere else, and using global vars is my least favorite option.

I've tried various things, which failed, for instance:

EVT_BUTTON($frame, $button, \&onClick(arg1,arg2));

I think the solution lies in the understanding of how Perl reference work, as we are passing \&onClick with the \& indicator in front. But I lack knowledge in that aspect of Perl...

Any help is appreciated

mephesis
  • 115
  • 8

1 Answers1

3

Try this,

EVT_BUTTON($frame, $button, sub{ onClick($arg1,$arg2) });

it gives reference to anon subroutine, which calls then onClick sub, and it is equivalent to:

my $subref = sub{ onClick($arg1,$arg2) };
EVT_BUTTON($frame, $button, $subref);
mpapec
  • 50,217
  • 8
  • 67
  • 127
  • Wow.. that actually worked! Thanks! Can you explain the logic behind this solution? – mephesis May 14 '14 at 20:27
  • The anonymous subroutine that access lexical variables ($arg1, $arg2) outside its scope is called a closure. – dolmen Oct 02 '14 at 16:09