http://qs321.pair.com?node_id=1176994

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

Hello monks, trying to run a system command using a file handle and insert its output to a txt box. Ive tried fileevent and fork. Both failed miserably.

my $mw = MainWindow->new; my $b1 = $mw->Button(-text => 'Go', -command => sub{&go})->pack(); my $b2 = $mw->Button(-text => 'Exit', -command => sub{exit} )->pack(); my $text = $mw->Scrolled('Text',-scrollbars=>'e')->pack; MainLoop; sub go { my $pid = fork(); if($pid == 0) { open(H, "nmap -A localhost |"); while(<H>) { $text->insert('end',$_); } } }

Thanks, -rjb

Thank you for the replies monks! It's working now...

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::IO; use Tk::ROText; my $entry_var=''; my $pid; my $mw = MainWindow->new(-title => 'Tk::IO'); $mw->geometry('570x500'); my $top_frame = $mw->Frame( -relief => 'groove', )->pack(-side=> 'top', -fill => 'x'); my $top_frame_left = $top_frame->Frame( -relief => 'groove', )->pack(-side => 'left',-fill => 'x'); my $top_frame_right = $top_frame->Frame( -relief => 'groove', )->pack(-side => 'right', -fill=>'x'); my $bot_frame = $mw->Frame( -relief => 'groove', -borderwidth => 5 )->pack(-side=> 'bottom', -expand => 1,-fill => 'both'); my $clear_button = $top_frame_left->Button( -text => 'Clear', -command => sub { &clear_text } )->pack(-side => 'left'); my $start_button = $top_frame_left->Button( -text => 'Start', -command => sub { &go } )->pack(-side => 'left'); my $stop_button = $top_frame_left->Button( -text => 'Stop', -state => 'disabled', -command => sub { &stop } )->pack(-side => 'left'); my $label = $top_frame_left->Label( -text => 'Enter Host' )->pack(-side => 'left'); my $entry = $top_frame_left->Entry( -textvariable => \$entry_var )->pack(-side => 'left'); my $exit_button = $top_frame_right->Button( -text => 'Exit', -command => sub { exit } )->pack(-side => 'right'); my $text_box = $bot_frame->Scrolled( 'ROText', -wrap => 'word', -font => 'rk24', -background => 'black', -foreground => 'green', -scrollbars => 'se' )->pack(-expand => 1, -fill => 'both'); MainLoop; sub go { if($entry_var eq ''){ $text_box->insert('end',"No host specified\n"); return; } $start_button->configure(-state => 'disabled'); $stop_button->configure(-state => 'active'); my $usr_command = Tk::IO->new( -linecommand => sub { $text_box->insert('end', shift()) }, -childcommand => sub { $text_box->yview('end'); $start_button->configure(-state => 'active'); $stop_button->configure(-state => 'disabled')}); $pid = $usr_command ->exec("nmap $entry_var 2>&1") or die $!; } sub stop { kill INT => $pid if kill 0 => $pid; $start_button->configure(-state=>'active'); $stop_button->configure(-state=>'disabled'); $text_box->insert('end', "Cancelled...\n"); $text_box->yview('end'); } sub clear_text { $text_box->delete('0.0','end'); }