I'd use Tk::after - it has millisecond granularity.
While I appreciate that the code you posted may well be a highly simplified version of your real application, you may not need Tk::fileevent at all. See my example code where it wasn't needed.
#!/usr/bin/env perl
use strict;
use warnings;
use Tk;
my $mw = MainWindow->new();
my $action_F = $mw->Frame()->pack(-side => 'bottom');
$action_F->Button(-text => 'Exit', -command => sub { exit })->pack;
my $text_F = $mw->Frame()->pack(-fill => 'both', -expand => 1);
my $ls_T = $text_F->Scrolled('Text', -scrollbars => 'osoe', -wrap => '
+none');
$ls_T->pack(-fill => 'both', -expand => 1);
my $out_win = $ls_T;
my $tid;
my $cmd = 'ls -al 2>&1';
my $repeat = 6;
my $delay = 100;
$tid = $mw->repeat($delay => [\&update_ls, \$out_win, \$tid, \$cmd, \$
+repeat]);
MainLoop;
{
my $updates = 0;
sub update_ls {
my ($text_ref, $tid_ref, $cmd_ref, $repeat_ref) = @_;
$$text_ref->insert(end => scalar(localtime) . "\n" . qx{$$cmd_
+ref});
if (++$updates >= $$repeat_ref) {
my $times_format = '(Usr: %d, Sys: %d, ChUsr: %d, ChSys: %
+d)';
$$text_ref->insert(end => sprintf $times_format => times);
$$tid_ref->cancel;
}
$$text_ref->yview('end');
return;
}
}
Notes:
-
From Tk::after, I've used the repeat() method to set it up; and the cancel() method when the callback's been called 6 times.
[Beyond mirroring the "for my $i ( 0 .. 5) ..." (in your posted code), the 6 is not special.]
-
Notice how all the callback arguments are references. This gives you a lot more flexibility: you could, for instance, change the delay, the command, or even the Tk::Text widget displaying the output.
-
The defaults for -width and -height are 80 and 24, respectively: I've left those two out.
I've made the -scrollbars optional (osoe).
-
The code I've posted is complete, working and tested on a directory with 412 entries.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.