#!/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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With