Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing commands using Button widget in Perl-Tk

Tags:

linux

perl

perltk

#!/usr/local/bin/perl
use Tk;
# Main Window
$mw = new MainWindow;
$label = $mw -> Label(-text=>"Hello World") -> pack();
$button = $mw -> Button(-text => "Quit",
                -command => sub { exit }) -> pack();
MainLoop;

In this code when the button $button is pressed it closes the program. Because it executes the exit command. I want to modify the code so that when the user clicks on the button it will flush the iptables rule (iptables -F). How can I do this?

I tried this:

$button = $mw -> Button(-text => "Flush the rules",
                    -command => system ( iptables -F )) -> pack();

Why isn't this working? Should I have to make a subroutine for it (then writing the iptables -F command there) and then call that subroutine? Can't I directly put the command as I did in above code?

like image 320
Chankey Pathak Avatar asked Jan 19 '26 16:01

Chankey Pathak


1 Answers

You need to specify a code reference - a callback - which will be executed when the button is pressed, so yes you should place your system call in a sub { }.

What you've written is a call to system() at the point that the Button is defined, so you're specifying the return value from system() as the coderef for the callback - which won't work. The system() function will be called when Button is defined, not when its pressed - which isn't what you want.

like image 69
martin clayton Avatar answered Jan 21 '26 07:01

martin clayton