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'); }

In reply to Perl Tk nonblocking by rjbuckley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.