#!/usr/bin/perl use strict; use warnings; use Carp; use IO::Pipe; {# Main if ((scalar @ARGV == 1) and ($ARGV[0] eq '--no-gui')) { printer(); } elsif ((scalar @ARGV >= 1) and ($ARGV[0] ne '--no-gui')) { print "USAGE: $0 [--no-gui]\n"; exit 1; } my $err_pipe = IO::Pipe->new() or croak "Could not create pipe: $!"; my $out_pipe = IO::Pipe->new() or croak "Could not create pipe: $!"; if (my $pid = fork ()) { # Child tk_init($err_pipe, $out_pipe); } else { # Father croak "Could not fork: $!" unless defined $pid; $err_pipe->writer(); $out_pipe->writer(); open STDERR, ">&" . $err_pipe->fileno() or croak "Could not redirect: $!"; open STDOUT, ">&" . $out_pipe->fileno() or croak "Could not redirect: $!"; printer(); } } sub tk_init { use Tk; use Tk::ROText; my $err_pipe = shift @_; my $out_pipe = shift @_; $err_pipe->reader(); $err_pipe->blocking(0); $out_pipe->reader(); $out_pipe->blocking(0); my $mw = MainWindow->new(); $mw->Label(-text => 'STDOUT')->pack(-side => 'top', -anchor => 'w'); my $ro_out_text = $mw->Scrolled('ROText', -scrollbars => 'osoe', -background => 'white', -width => 50, -height => 15, )->pack(-side => 'top'); $mw->Label(-text => 'STDERR')->pack(-side => 'top', -anchor => 'w'); my $ro_err_text = $mw->Scrolled('ROText', -scrollbars => 'osoe', -background => 'white', -width => 50, -height => 15, )->pack(-side => 'top'); $mw->repeat(250, [\&update_text_widget, $ro_out_text, $out_pipe]); $mw->repeat(250, [\&update_text_widget, $ro_err_text, $err_pipe]); $mw->update(); MainLoop(); } sub update_text_widget { my $widget = shift @_; my $handle = shift @_; # slurp data my @in = <$handle>; my $str = join('', @in); $widget->insert('end', $str); return; } sub printer { use Time::HiRes qw(usleep); my $errcount = 0; my $count = 0; while (1) { if (rand(2) % 2 == 1) { $count++; print "STDOUT: Count = $count\n"; } else { $errcount++; print {*STDERR} "STDERR: Count = $errcount\n"; } usleep (rand(500000) + 500000); } }