Search code examples
c++perlwxwidgetswxperl

How do I pass additional arguments in wxperl EVT_BUTTON


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


Solution

  • 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);