# GUI for _LF_aligner_2012_01_16.pl package MyGuiStdin; our @ISA = qw[ Thread::Queue ]; sub TIEHANDLE { bless $_[1], $_[0]; } sub READLINE { $_[0]->dequeue(); } package MyGuiStdout; our @ISA = qw[ Thread::Queue ]; sub TIEHANDLE { bless $_[1], $_[0]; } sub PRINT { $_[0]->enqueue( join ' ', @_[ 1 .. $#_ ] ); } sub PRINTF { $_[0]->enqueue( sprintf $_[1], @_[ 2 .. $#_ ] ); } package MyGui; use strict; use warnings; use threads; use Thread::Queue; my $Qin = new Thread::Queue; my $Qout = new Thread::Queue; tie *STDIN, 'MyGuiStdin', $Qin; tie *STDOUT, 'MyGuiStdout', $Qout; # everything up to this point is pure black magic sub gui { require Tk; #OK require Tk::DialogBox; #OK my $mw = Tk::MainWindow->new; #OK my $lb = $mw->Listbox( -width => 80, -height => 24 )->pack; my $ef = $mw->Entry( -width => 70, -takefocus => 1 )->pack( -side => 'left' ); #OK my $enter = sub { # I guess this is to avoid the "will not stay shared" error due to nested subs $Qin->enqueue( $ef->get ); # pass the entry field's content to the STDIN of the .pl $ef->delete(0, 'end' ); # delete all the text from the entry field 1; # what does this do? }; my $do = $mw->Button( -text => 'go', -command => $enter)->pack( -after => $ef ); #OK $mw->bind( '', $enter ); #Ok $ef->focus( -force ); #OK my $doStdout = sub { if( $Qout->pending ) { # no clue my $output = $Qout->dequeue; # read STDOUT of .pl into var $lb->insert( 'end', $output ) ; # print STDOUT of .pl in listbox $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 } 1;