For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > May 2005 > Re: Tk:Text and reading a never ending file [Solved]









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Tk:Text and reading a never ending file [Solved]
Richard

2005-05-27, 8:56 am

Thanks the the great answers guys.

(I should have read the book a little futher :-(


"Richard" <user@host.com> wrote in message
news:aPlle.424$Nb2.7314@news1.nokia.com...
> Hi
> This little problem is related to Tk::Text on Linux. I want to be able to
> monitor the text output from a script. In the same way as tail -f

<filename>
> will do. That is the file being displayed is still being written to, and

as
> new text is written to the file it will be output in the text widget.
>
> What happen is that the script reads the file and outputs it to the screen
> via the print (the print was to check something was happenuing :-) but
> nothing
> is visable in the text widget. When he end of file is reached the script
> appears to hang.
>
> I'm assuming I'm going to need to do a fork or use Threads to read the

file
> and
> allow the script to carry on.
>
> Any ideas ?
>
>
>
>
> use strict;
> use warnings;
>
> use Tk;
> use Tk::widgets qw(MainWindow Text);
>
>
>
> my $mw = MainWindow->new;
> my $text = $mw->Scrolled('Text', -wrap=>'none', -scrollbars=>'osoe')
> ->pack();
>
> my $start = $mw->Button(-text=>'Start', -height=>1, -command=>[\&monitor,
> $text])
> ->pack();
>
> MainLoop();
>
> sub monitor {
> my ($text) = @_;
>
> # this bit is from that great book Perl/Tk by Steve Lidie & Nancy Walsh
> $text->delete("1.0", 'end');
>
> my $cmd = 'tail -f <some file>';
> if (!open IN, "$cmd |"){
> $text->inset('end', "Cannot run the command '$cmd' : $!\n");
> return;
> }
>
> while (<IN> ){
> $text->insert('end', $_);
> print "$_"; # so you can see the commasnd is working
> }
> }
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com