Hi, I'm hoping you can help me on something that has bugged me for a couple of years now. I have a perl/TK script which runs a main perl script with a basic TK front end. The problem I just cannot seem to crack is how to run it and achieve three things:
1) - all STDOUT is printed to a text widget (including from any system commands that are all called)
2) - the Tk front end doesn't just hang whilst the routine is running (and it can run for a couple of days sometimes, depending on what system commands are being run).
3) - it must work on Windows not just Unix
Below is a simple test code which runs what I currently do (badly)
#!/usr/bin/perl -w # use strict; # use Encode::Unicode; use Tk; # open(H, "tail -f -n 25 $ARGV[0]|") or die "Nope: $!"; # open(STDOUT, "+> |") or die "Nope: $!"; # open STDOUT, '+<', \&test or die "Can't open STDOUT: $!"; $mw=MainWindow->new(); # create main window, and stretch it to fill sc +reen minus a bit $mw->title("GRM Consulting Ltd. - GENESIS Optimisation Coupling Tool v +1.0"); $mw->geometry("800x600+100+100"); my $t = $mw->Text(-width => 80, -height => 25, -wrap => 'none'); $t->pack(-expand => 1); # $mw->fileevent(\*STDOUT, 'readable', [\&fill_text_widget, $t]); $mw->update; test(); MainLoop; sub fill_text_widget { my($widget) = @_; my($stat, $data); $stat = sysread STDOUT, $data, 4096; die "sysread error: $!" unless defined $stat; $widget->insert('end', $data); $widget->yview('end'); } ## Test routine to simulate my main program execution sub test { for ($i=0; $i<3; $i++ ) { print "testline $i\n"; sleep 1; } $command="dir"; system("$command"); for ($i=0; $i<3; $i++ ) { print "testline1 $i\n"; sleep 1; } die; }
As you'll see by the commented out line I have been trying to use the filevent command to pipe things and have had a little bit of success but not really too much. If you can help with this it is going to solve a long standing problem in my understanding of how perl and tk talk.
Many thanks
Martin Gambling (<- hence the Gambling user name!!)

In reply to Executing pipe STDOUT from a subroutine to Widget by Gambling1uk

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.