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

Greetings all,
I'm back to fiddling with Perl/Tk stuff, and my recent project was to dink around with making a telnet client. Easy enough, really.
What I can't quite work out is how to smoothly read data from the server, while in the Perl/Tk event driven environment.
I mean, if I'm always waiting on events to happen in the Tk environ... how am I supposed to also get data from the server? What I have now works quite well, considering how nasty its implemented, but whenever the check is done, the whole app "pauses"... And thats no good.
$widget->repeat(2000, \&CheckInput); MainLoop; sub CheckInput { foreach my $data ($select->can_read(1)) { my $rv = $data->recv(my $buffer, POSIX::BUFSIZ, 0); unless (defined($rv) && length $buffer) { &WWrite("\n[Disconnected]\n"); exit(0); } &WWrite(&filter($buffer)); } $widget->update(); }
Functional, but oh so nasty.
What's the right way - or at least a BETTER way - to do what I'm trying to do?
Thanks Monks,

JP,
-- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

  • Comment on Perl/Tk telnet client - reading server input in an Event Driven environment
  • Download Code

Replies are listed 'Best First'.
Re: Perl/Tk telnet client - reading server input in an Event Driven environment
by Ido (Hermit) on Mar 23, 2002 at 21:52 UTC
    You might want to try the fileevent method of tk:
    $widget->fileevent(fileHandle,readable?,callback?)

    This command is used to create file event handlers. A file event handler is a binding between a filehandle and a callback, such that the callback is evaluated whenever the filehandle becomes readable or writable
      Hi!

      I've never used the fileevent() so i may be wrong, but as for me 'readable' or 'writable' means, that the file is accessible in such mode - it doesn't mean that there is some data to read.
      I found this problem using IO::Select's can_read() function. It returns all the file handles that are 'readable', but you still won't know which of them has an input waiting... Maybe it would work better for me if i used flock() on those files between data reads? Never mind - i've got a work-around ;-)

      Greetz, Tom.
        From the Tk::fileevent doc: A filehandle is considered to be readable if there is unread data available on the underlying device.