A package, BMORROW::Tie::TkWatch, to give "a basically command-line program a simple GUI". Reproduced here with the author's consent.
The module
package BMORROW::Tie::TkWatch;
use warnings;
use strict;
our @EXPORT_OK = qw/Run Dialog Update/;
use base qw/Exporter/;
use Tk;
use Tk::Dialog;
use Scalar::Util qw/openhandle/;
my %TK;
$TK{mw} = Tk::MainWindow->new(
-title => 'Perl',
);
$TK{font} = $TK{mw}->Font(
-family => 'Bitstream Vera Sans Mono',
-size => 10,
);
$TK{out} = $TK{mw}->Scrolled(
ROText =>
-font => $TK{font},
-width => 80,
-height => 35,
-scrollbars => 'osoe',
)->pack(
-fill => 'both',
-expand => 'y',
);
$TK{out}->tag(configure => out => -foreground => 'black');
$TK{out}->tag(configure => err => -foreground => 'red');
$TK{out}->focus;
sub TIEHANDLE {
my $c = shift;
return bless \@_, $c;
}
sub PRINT {
my $s = shift;
my ($o, $t, $h) = @$s;
my $txt = do {
no warnings 'uninitialized';
(join $,, @_) . $\
};
openhandle $h and print $h $txt;
$o->insert(end => $txt, $t);
$o->see('end');
$o->update;
}
tie *STDOUT, __PACKAGE__, $TK{out}, 'out';
open my $OERR, '>&', \*STDERR;
tie *STDERR, __PACKAGE__, $TK{out}, 'err', $OERR;
sub Run (&$) {
my $sub = shift;
$TK{mw}->configure(-title => shift);
$TK{mw}->after(0, $sub);
Tk::MainLoop;
}
sub Dialog {
my $ans = $TK{mw}->Dialog(@_)->Show();
$TK{mw}->update;
return $ans;
}
sub Update {
$TK{mw}->update;
}
sub Tk::Error {
my ($mw, $err) = @_;
print STDERR $err;
}
1;
__END__
=head1 AUTHOR, WARRANTY, COPYRIGHT, & LICENCE INFO
Copyright 2007 Ben Morrow
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.5
or, at your option, any later version of Perl 5 you may have
available.
=cut
Usage
#!/usr/bin/perl
use BMORROW::Tie::TkWatch qw/Run/;
Run {
print "Hello world!\n";
warn "Houston, we have a problem";
} 'My Perl App';
__END__
More Info
This was recently
posted on
clpmisc by
Ben Morrow. (
Available from
Google Groups too.) I asked for permission to reproduce it here and he agreed. I only added the copyright notice as per the author's request. See the original thread linked above for more info.