Here is an updated version that looks for two specific prompts coming from STDOUT and popping up a dialog to deal with them. The implementations of those dialogs is crude --no validation; minimalistic gui; my Tk is pretty simplistic -- but they are a demonstration only. You can enhance to your hearts desire.
Again, the simplistic command line app:
#! perl -slw
use strict;
use threads;
our $GUI //= 0;
if( $GUI ) {
require 'MyGui.pm';
async( \&MyGui::gui )->detach;
}
while( 1 ) {
printf 'Enter three, 2-digit numbers: ';
my $in = scalar <STDIN>;
exit if $in =~ '!bye';
print 'You entered: ', $in;
printf 'Enter a date and time: ';
$in = scalar <STDIN>;
exit if $in =~ '!bye';
print 'You entered: ', $in;
}
Updated: Improved the code below a little.
And the gui module to service it it when the -GUI command line option is supplied:
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;
sub gui {
require Tk;
require Tk::DialogBox;
my $mw = Tk::MainWindow->new;
my $lb = $mw->Listbox( -width => 80, -height => 24 )->pack;
my $ef = $mw->Entry( -width => 70, -takefocus => 1 )->pack( -side
+=> 'left' );
my $enter = sub {
$Qin->enqueue( $ef->get );
$ef->delete(0, 'end' );
1;
};
my $do = $mw->Button( -text => 'go', -command => $enter)->pack( -a
+fter => $ef );
$mw->bind( '<Return>', $enter );
$ef->focus( -force );
my $doStdout = sub {
if( $Qout->pending ) {
my $output = $Qout->dequeue;
$lb->insert( 'end', $output ) ;
$lb->see( 'end' );
if( $output eq 'Enter three, 2-digit numbers: ' ) {
my $db = $mw->DialogBox( -title => 'Three 2 digit numb
+ers', -buttons => [ 'Ok' ] );
my $e1 = $db->add( 'Entry', -width => 3 )->pack( -side
+ => 'left' );
my $e2 = $db->add( 'Entry', -width => 3 )->pack( -side
+ => 'left', -after => $e1 );
my $e3 = $db->add( 'Entry', -width => 3 )->pack( -side
+ => 'left', -after => $e2 );
$e1->focus( -force );
$db->Show;
my $input = sprintf "%2d %2d %2d", $e1->get, $e2->get,
+ $e3->get;
$Qin->enqueue( $input );
}
elsif( $output eq 'Enter a date and time: ' ) {
my $db = $mw->DialogBox( -title => 'Date&time', -butto
+ns => [ 'Ok' ] );
my $day = $db->add( 'Entry', -width => 2 )->pack( -sid
+e => 'left' );
my $mon = $db->add( 'Entry', -width => 2 )->pack( -sid
+e => 'left', -after => $day );
my $year= $db->add( 'Entry', -width => 4 )->pack( -sid
+e => 'left', -after => $mon );
my $hours = $db->add( 'Entry', -width => 3 )->pack( -s
+ide => 'left' );
my $mins = $db->add( 'Entry', -width => 3 )->pack( -s
+ide => 'left', -after => $hours );
my $secs = $db->add( 'Entry', -width => 3 )->pack( -s
+ide => 'left', -after => $mins );
$day->focus( -force );
$db->Show;
my $input = sprintf "%2d/%02d/%02d %2d:%02d:%02d",
$day->get, $mon->get, $year->get, $hours->get, $mi
+ns->get, $secs->get;
$Qin->enqueue( $input );
}
}
};
$mw->repeat( 500, $doStdout );
Tk::MainLoop();
}
1;
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.