Re: Dazed and confused about menus



David Sword said:
Everything works fine, except for attaching the callback to run the
programs - it always contains the details of the last menuitem created,
as opposed to the one selected, and I don't understand how to make this
work (or even if it CAN work!)

the answer is perl scoping rules.


sub LoadMenus{
    my @data;
    my @data1;
,,,
      if($data[$cntr][1] eq 'P'){
          $progname  = $subname;
          $MenuItem[$cntr]->signal_connect('activate' => sub{
\&RunProg(@_,$progname)} );
      }else{
...
      }
...
    }
...
}

$progname appears to be a global, in which case, the closure will always see
the last value set.

either use a lexical in the closure, like this:


        if($data[$cntr][1] eq 'P'){
            my $progname  = $subname;
            ^^^
            $MenuItem[$cntr]->signal_connect('activate' =>
sub{\&RunProg(@_,$progname)} );
        }else{

or pass the name string as the user data argument to the signal handler, like
this:

        if($data[$cntr][1] eq 'P'){
            # by passing subname as the user data, it will be
            # appended to the closure's @_.
            $MenuItem[$cntr]->signal_connect('activate' => sub{\&RunProg(@_)},
                            $subname );
        }else{

-- 
muppet <scott at asofyet dot org>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]