Search code examples
perltk

Perl TK multiple commands on a single button


I would like to do is to have a button to close my window (button_window), but also call a function (user_info):

my $btn = $main -> Button (-text => 'Start',
-command => sub {$button_window -> destroy},
-command => \&user_info)
-> pack ();

its executing only the last command thanks in advance


Solution

  • The sub can take any number of calls to other subs.

    my $btn = $main->Button(
        -text    => 'Start',
        -command => sub {
            user_info();
            # do something else...
            $button_window->destroy;
        },
    )->pack();
    

    It's executing only the last command because a hash parameter can have only one '-command' key, so is overwritten.