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