my $doStdout = sub {
## $Qout is a [Thread::Queue] object onto which anything that is printed to stdout is enqued by the "black magic".
## It is created by the statement my $Qout = new Thread::Queue;.
## If you look up the documentation for that module you'll see that the pending() method returns
## the count of 'things' in the the queue. Using that information, the next line of code reads:
## If there is anything in the queue -- ie. if the parent program has printed anything to stdout
if( $Qout->pending ) { # no clue
my $output = $Qout->dequeue; # read STDOUT of .pl into var ## YES.
$lb->insert( 'end', $output ) ; # print STDOUT of .pl in listbox ## YES.
## We've inserted the next item into the listbox,
## but this addition may have gone 'off the bottom' of the visible part of the listbox
## so ask the listbox to redraw itself to ensure that we can 'see' the 'end'
## ie. the last item in the listbox. Ie. the one we just added.
$lb->see( 'end' ); # What's this?
if( $output eq 'whatever' ) {
# actual Tk code
}
}
};
####
$mw->repeat( 500, $doStdout ); # execute $doStdout once every 500 millisec...? but why mw->
Tk::MainLoop(); #OK
}
##
##
use Thread::Queue;
## A queue, that we tie to the main programs stdin.
## Anything our Tk code $Qin->enqueues( ... ) (writes) to this queue
## Will turn up in the main program when it reads from stdin (Eg. while( my $line = ) etc.)
my $Qin = new Thread::Queue;
## And another queue that we tie to the main programs stdout.
## Anything the main program prints to stdout, we can access in our Tk code
## by reading (my $line = $Qout->dequeue()) from this queue
my $Qout = new Thread::Queue;
## So, we've created the means for bidirectional communications between us and the main program
## And now we need to (transparently) persuade the main program to use them instead of the console.
## Which we do by [tie]ing the standard handles to our newly created queues.
tie *STDIN, 'MyGuiStdin', $Qin;
tie *STDOUT, 'MyGuiStdout', $Qout;
# everything up to this point is pure black magic
##
##
sub PRINT { $_[0]->enqueue( join ' ', @_[ 1 .. $#_ ] ); }
##
##
package MyGuiStdin;
our @ISA = qw[ Thread::Queue ];
sub TIEHANDLE { bless $_[1], $_[0]; }
sub READLINE { $_[0]->dequeue(); }