gator456 has asked for the wisdom of the Perl Monks concerning the following question:

I am looking for some help with a Perl/Tk script I am writing.

I want to wrap some Tk around a UNIX text based program. The UNIX program takes input from STDIN as a command line and results are returned to STDOUT. (The UNIX program is TCL based and behaves very similiar to `tclsh`)

What I had in mind was to create a TK-Entry box for input, then feed that to the UNIX program. Then the UNIX program would feed its output to a TK-text box.

What would be the perl code to start the UNIX program and send data back and forth to it? Is it a PTTY?

If there are some examples out there, please let me know. I think the Tk would look like this:

#!/usr/bin/perl use Tk; require Tk::NoteBook; my $mw = MainWindow->new; $nb = $mw->NoteBook()->pack(-side => 'bottom', -expand => 1, -fill => +'both'); $entry = $mw->Entry()->pack(-side => 'bottom', -expand => 1, -fill => +'both'); $tab1 = $nb->add('tab1', -label => 'tab1'); $t1 = $tab1->Scrolled("Text")->pack(-expand => 1, -fill => 'both'); MainLoop;

Replies are listed 'Best First'.
Re: Perl/Tk Unix Question
by zentara (Cardinal) on May 30, 2008 at 15:37 UTC
    Here is an example, which is a stripped down version of Tk::ExecuteCommand. I run a bash shell, but you can put in your program directly. Some programs do need a pty (rarely), so see if you can get this to run first.
    #!/usr/bin/perl use warnings; use strict; use Tk; use IPC::Open3; require Tk::ROText; $|=1; my $mw = new MainWindow; my $entry=$mw->Entry(-width => 80)->pack; $mw->Button(-text => 'Execute', -command => \&send_to_shell)->pack; my $textwin =$mw->Scrolled('ROText', -width => 80, -bg =>'white', -height => 24, )->pack; $textwin->tagConfigure( 'err', -foreground => 'red' ); my $pid = open3( \*IN, \*OUT, \*ERR, '/bin/bash' ) or warn "$!\n"; $mw->fileevent( \*OUT, readable => \&read_stdout ); $mw->fileevent( \*ERR, readable => \&read_stderr ); $entry->focus; MainLoop; sub read_stdout { if( sysread( OUT, my $buffer, 1024 ) > 0 ){ $textwin->insert( 'end', $buffer ); $textwin->see('end'); } } sub read_stderr { if( sysread(ERR, my $buffer, 1024 ) > 0 ){ $textwin->insert( 'end', $buffer, 'err' ); $textwin->see('end'); } } sub send_to_shell { my $cmd= $entry->get(); print IN "$cmd\n"; }

    I'm not really a human, but I play one on earth CandyGram for Mongo
Re: Perl/Tk Unix Question
by pc88mxer (Vicar) on May 30, 2008 at 15:29 UTC
Re: Perl/Tk Unix Question
by psini (Deacon) on May 30, 2008 at 15:14 UTC

    See IPC::Open2. It allows you to execute a command piping you its stdin & stdout.

    Careful with that hash Eugene.