Update: Simplified problem temporarily by removing requirement of stderr

A bucket-load of XP to the monk that can make this work:

use strict; $|++; if( $ARGV[0] eq 'child' ) { sleep 1; print STDOUT "stdout1\n"; sleep 1; print STDOUT "stdout2\n"; sleep 1; print STDOUT "stdout3\n"; sleep 1; print STDOUT "stdout4\n"; exit; } use Tk; use Tk::ROText; my $mw = MainWindow->new; my $text = $mw->ROText()->pack( -fill => 'both', -expand => 1 ); use IO::Handle; my $stdout = IO::Handle->new(); my $pid = open( $stdout, "$0 child |" ) || die; $stdout->autoflush( 1 ); $mw->fileevent( $stdout, readable => \&read_stdout ); MainLoop; sub read_stdout { print "read_stdout()\n"; my $num = $stdout->sysread( my $buffer, 1024 ); print "sysread() got $num bytes:\n[$buffer]\n"; $text->insert( 'end', $buffer ); }
Original code:
use strict; $|++; if( $ARGV[0] eq 'child' ) { sleep 1; print STDOUT "stdout1\n"; sleep 1; print STDERR "stderr1\n"; sleep 1; print STDOUT "stdout2\n"; sleep 1; print STDERR "stderr2\n"; exit; } use Tk; use Tk::ROText; my $mw = MainWindow->new; my $text = $mw->ROText()->pack( -fill => 'both', -expand => 1 ); $text->tagConfigure( 'red', -foreground => 'red' ); use IO::Handle; my $stdin = IO::Handle->new(); my $stdout = IO::Handle->new(); my $stderr = IO::Handle->new(); use IPC::Open3; my $pid = open3( $stdin, $stdout, $stderr, "$0 child" ) || die; $stdin->close; $stdout->autoflush( 1 ); $stderr->autoflush( 1 ); $mw->fileevent( $stdout, readable => \&read_stdout ); $mw->fileevent( $stderr, readable => \&read_stderr ); MainLoop; sub read_stdout { print "read_stdout()\n"; my $num = $stdout->sysread( my $buffer, 1024 ); print "sysread() got $num bytes:\n[$buffer]\n"; $text->insert( 'end', $buffer ); } sub read_stderr { print "read_stderr()\n"; my $num = $stderr->sysread( my $buffer, 1024 ); print "sysread() got $num bytes:\n[$buffer]\n"; $text->insert( 'end', $buffer, 'red' ); }
This is a simplified exmaple of what i want to achieve, which basically is to see the stdout and stderr of a program in a Tk widget. Note there are numerous problems here, such as being able to perform a non-blocking read (on win32!), avoiding deadlock between stdout and stderr, and keeping the MainLoop running. You can assume the child is never going to need stdin, and will run for a long time (ie, a daemon).

- ><iper

use japh; print;

In reply to capture output of a daemon in a tk widget on win32 by xiper

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.