sashac88 has asked for the wisdom of the Perl Monks concerning the following question:

Hello great monks!
Is it possible to add something dynamic, like the output of
the `tail -f filename` command to the Tk::Scrolled
widget?
Below is an example of static content:
#!/usr/bin/perl use Tk; use strict; my $mw=MainWindow->new; my $scroll=$mw->Scrolled(qw/Text -relief sunken -borderwidth 2 -setgri +d true -height 30 -scrollbars e/); $scroll->pack(qw/-expand yes -fill both/); $scroll->insert('0.0','some test'); MainLoop
Is it possible to replace this static content with the
output mentioned above?
Thanks a lot in advance. Thanks a lot zentara!
I noticed that the prompt isn't returned upon closing the
MainWindow. Do you know how to overcome this?

Replies are listed 'Best First'.
Re: Adding the output of tailf -f filename to Tk widget (scrolled)
by zentara (Cardinal) on Feb 13, 2007 at 11:37 UTC
    There are a few variations on this, but basically you do a piped-open of the tail command, then run the filehandle thru Tk::fileeevnt. But here is an even easier way.
    #!/usr/bin/perl use strict; use Tk; use IO::Handle; my $H=IO::Handle->new; open($H,"tail -f -n 50 z.txt |") or die $!; my $main = MainWindow->new; my $t=$main->Text(-wrap=>'none')->pack(-expand=>1); $main->fileevent(\*$H,'readable',[\&fill,$t]); MainLoop; sub fill { my ($w)=@_; my $text; my $text =<$H>; $w->insert('end',$text); $w->yview('end'); }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Adding the output of tailf -f filename to Tk widget (scrolled)
by Anonymous Monk on Feb 13, 2007 at 07:31 UTC