Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Perl::TK - fileevent and script execution theory

by crabbdean (Pilgrim)
on Mar 14, 2004 at 12:19 UTC ( #336482=note: print w/ replies, xml ) Need Help??


in reply to Perl::TK - fileevent and script execution theory

Okay, thanks merlyn and zentara in this node for their responses, it very much helped. Although what I was attempting to achieve could be seen in the TK::ExecuteCommand module I wanted to know and write how to output an open stream to a widget (notably a text Widget). By open stream I mean STDOUT or any open pipe, or Filehandle. The rest you can read in the responses and discussion of this node.

Outputting to a text widget is all fine and dandy if what you are reading and outputting is small, because the population of the data to gui is relatively fast. But try executing a script or outputting a large amount of data like a listing of your entire hard drive and your gui freezes until it finishes the read. For all intensive purpose your gui looks like its crapped itself, while its off busily working in the background. The event driven nature of TK means it will process this event before continuing and updating the display or the next event.

The TK::fileevent module is designed to execute a callback when a FileHandle becomes either writable or readable. Great sounds good!! Although, my problem came in the implementation of this, which you can read about in this node. Finally here is the solution below. The "$widget->idletasks" method helps with the updating of the display when the widget changes or in this case is populated with more data, which fixes the apparant freezing problem. Calling "idletasks" for the MainWindow or the text widget within the callback both worked although on the Text Widget would require less redraw and is most likely faster, hence the solution below. I found trying to call "idletasks" within the MainLoop didn't work.

Below is the final solution. Hope that helps.


Dean
The Funkster of Mirth
Programming these days takes more than a lone avenger with a compiler. - sam
RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers
#!/usr/bin/perl use warnings; use strict; use Tk; use IO::Handle; open(CHILD, "e:\\cygwin\\bin\\ls -laR c: |") or die "Can't open: $!"; my $mw = new MainWindow; my $t = $mw->Scrolled("Text", -width => 80, -height => 25, -wrap => 'n +one'); $t->pack(-expand => 1); CHILD->autoflush(1); $mw->fileevent('CHILD', 'readable', [\&fill_text_widget, $mw]); MainLoop; sub fill_text_widget { my ($widget) = @_; if (eof(CHILD)) { $widget->fileevent('CHILD', "readable", undef); # cancel bindi +ng return ; } if (sysread ('CHILD', $_, 1028)) { $t->insert('end', $_); # Append the data read $t->yview('end'); $widget->bell; } else { # sysread returned undef. Problem with file $widget->fileevent('CHILD', "readable", undef); # cancel bindi +ng } $t->idletasks; }


Comment on Re: Perl::TK - fileevent and script execution theory
Download Code
Re: Re: Perl::TK - fileevent and script execution theory
by crabbdean (Pilgrim) on Mar 25, 2004 at 00:27 UTC
    I've noticed in the above code that instead of exiting at the eof(CHILD) section it exists at the bottom "else" statement. All exiting commands I'm wanting to execute such as cleanup or "destroys" I'm putting here, which seems illogical. I would have thought it would exit at the "eof". Is there a reason for this?

    Update: It would seem more correct to put the whole bottom "sysread" if statement as and "else" to the "eof". See below.
    if (eof($handle)) { $widget->fileevent($handle, "readable", undef); # cancel bindi +ng return ; } else { if (sysread ($handle, $_, 128)) { $tx->insert('end', $_); # Append the data read $tx->yview('end'); } else { $tx->insert('end', times); $tx->yview('end'); $widget->fileevent($handle, "readable", undef); # cancel b +inding return; } }

    Dean
    The Funkster of Mirth
    Programming these days takes more than a lone avenger with a compiler. - sam
    RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://336482]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-07-29 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (220 votes), past polls