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

I'm having troubles figuring out how to do the following with Perl/Tk.
The source of the problem is the fact that once MainLoop is called, it seems as though the only things that happens can be as a result of user-interactions.
The problem is that I need to be able to do other stuff while MainLoop is running. Think of a Perl/Tk telnet client. Just because the user isn't doing anything, that doesn't mean nothing is happening. How would I read from the socket and print to a Label or Canvas while MainLoop is running?
My project is not as simple as a basic telnet client, but it makes for a good example. In my case, I'll be parsing everything read from the socket and then deciding what to do with it (usually displaying it in a Text box or on a Label).

Thanks (2^2^2^2^2)!
Daimun

Replies are listed 'Best First'.
Re: Perl/Tk Non-user Interactivity
by busunsl (Vicar) on Mar 20, 2001 at 12:03 UTC
    You can do this in a simple subroutine, you just have to call $MW->update() frequently, so that you Tk events will be processed.
      I saw the update method, but I just don't understand how to use it. I have not been able to find any examples.
        Suppose you read from that socket and do the processing in a sub.
        This sub is called somewhere in the main program, probably from a menu.
        You pass a reference to the MainWindow to the sub and inside the sub you call update(), like this:
        . . . my $menu = $menubar->Menubutton(qw/Name menu -tearoff 0 -text somethin +g -underline 0 -menuitems/ => [ [command => 'read socket', -command => [\&read_socket, $MW]], ] )->pack(-side => 'left'); . . . sub read_socket { my $MW = shift; while (some_condition) { do_something_with_socket; $MW->update(); } }
Re: Perl/Tk Non-user Interactivity
by the_slycer (Chaplain) on Mar 20, 2001 at 10:50 UTC
    Easiest thing would be to fork a child off to do the stuff that you want to do while the rest of the program is just waiting for user input.. if you need to have it update things in the Tk window I believe you could use Tk::Receive and send to complete this.
(jcwren) Re: Perl/Tk Non-user Interactivity
by jcwren (Prior) on Mar 20, 2001 at 19:08 UTC

    You could also use the afterIdle method. This allows a callback function to be executed when Tk enters the idle loop. However, if a user grabs a scroll bar with the mouse and honks it up and down, it's unlikely that you'll get into the idle loop.

    You could also combine this with after, which allows a callback function to be invoked 'n' milliseconds later. Just set 'n' to a low number, so your callback function gets executed often.

    --Chris

    e-mail jcwren
Re: Perl/Tk Non-user Interactivity
by Daimun (Novice) on Mar 20, 2001 at 10:01 UTC
    As a note, I'm holding out on threading as a last resort. Perl Threads don't seem too well ported at the moment, and would much rather do it in a linear fashion with the socket in non-blocking mode.
      Perl's various threading models are all experimental for good reason, and the final model in 6.0 is unlikely to match any of them.

      So I wouldn't use threading as a solution.