fork() is problematic with Tk applications, as the child process can't munge with the parent's window hierarchy. You could fork(), and then have the child feed the data over a pipe or socketpair...seems complicated though.

There is Tk::fileevent, which gets you most of the way there, but doesn't have a built-in mechanism to re-seek for the 'tail -f' functionality you need. Basically the fileevent fires on eof, so you would have to turn the fileevent on and off. Ick.

My last idea was to use Tk::After's repeat() functionality. Seems to work for me. This short example creates a listbox, and does something of a "tail -f /tmp/testfile" on it while still allowing other events to fire. (You can type in the entry box to verify this)

Here you go:

#!/usr/bin/perl use IO::File; use IO::Select; use Tk; my $FILE=IO::File->new("/tmp/testfile") or die; my $SEL=IO::Select->new(); $SEL->add($FILE); my $mw=Tk::MainWindow->new(); my $list=$mw->Listbox->pack(); my $entry=$mw->Entry->pack(); $list->repeat(500, sub { if ($SEL->can_read(0)) { while(<$FILE>) { chomp; $list->insert(end,$_); $list->see(($list->size)-1); } seek(FILE,0,1); } }); MainLoop;
Update: Depending on how fast the text is streaming, you may want to call Tk::DoOneEvent(0) in the while(<$FILE>) loop.

In reply to Re: running a loop in background... by kschwab
in thread running a loop in background... by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.