taking into account my current level. Well to help you understand a bit better, whenever you run something that can take time, and/or hang, like a ping, you need to separate that code from the gui eventloop code. This is usually done thru IPC, forking, or threads. That keeps the gui responsive.
So here is a simpler example of it. This code forks off the ping command, thru a piped open ( see "perldoc perlipc"), and then allows the tk eventloop to read the filehandle thru fileevent ( which is similar to C's select ). The problem with using Net::Ping is that it dosn't allow you direct access to the filehandles involved, like a piped open does. Then you need the hack workaround.
Here is a simpler version of using fileevent, for you to see the basics.
#!/usr/bin/perl -w
use Tk;
use strict;
my $host = 'localhost';
my $mw = MainWindow->new;
my $t = $mw->Scrolled("Text",-width => 80,
-height => 25, -wrap => 'none')->pack(-expand => 1);
my $button = $mw->Button(-text => 'Execute',
-command => \&ping_test)->pack;
MainLoop();
sub ping_test {
open (PINGTEST, "ping $host 2>&1 |") or die "can't fork: $!";
$mw->fileevent(\*PINGTEST, 'readable', [\&fill_text_widget,$t]);
$button->configure(-text => 'Stop',
-command => sub{
print "Close Filehandle\n";
close PINGTEST;# or die "bad ping: $! S?";
print "All done!\n";
});
}
sub fill_text_widget {
my($widget) = @_;
$_ = <PINGTEST>;
$widget->insert('end', $_);
$widget->yview('end');
}
|