G'day rjbuckley,

Welcome to the Monastery.

I've previously encountered the same issue as you with Tk::fileevent: when you've read everything from the filehandle, the GUI locks up. I don't know what's going on under the hood, so I can't provide an explanation of this behaviour; however, the general solution is to read from the filehandle until you get an undefined result and then delete the event handler:

$widget->fileevent($fh, readable => sub { if (defined(my $read = scalar <$fh>)) { # Do something with $read here } else { $widget->fileevent($fh, readable => ''); } });

Here's a somewhat more robust solution that:

[I wanted something like this for a future project, so it's a prototype of sorts. I'll be extending it for my own needs. Feel free to use all, some or none of the features.]

#!/usr/bin/env perl use strict; use warnings; use autodie qw{:all}; use Tk; { my $mw = MainWindow::->new(-title => 'Test `nmap` with Tk::fileeve +nt'); my $ctrl_F = $mw->Frame()->pack(-side => 'bottom'); my $cmd_F = $mw->Frame()->pack(-side => 'top'); my $text_F = $mw->Frame()->pack(-side => 'top', -fill => 'both', - +expand => 1); my $gui_data = { start_button_text => get_start_button_text(), stop_button_text => get_stop_button_text(), button_pack_opts => [-side => 'left', -padx => 10], }; $gui_data->{out_T} = $text_F->Scrolled('Text', -scrollbars => 'osoe', -wrap => 'none', -bg => '#ffffff' )->pack(-fill => 'both', -expand => 1); sub get_button_width (); $gui_data->{start_B} = $cmd_F->Button( -textvariable => \$gui_data->{start_button_text}, -state => 'normal', -width => get_button_width(), -command => [\&start_nmap, \$mw, $gui_data], )->pack(@{$gui_data->{button_pack_opts}}); $gui_data->{stop_B} = $cmd_F->Button( -textvariable => \$gui_data->{stop_button_text}, -state => 'disabled', -width => get_button_width(), -command => [\&stop_nmap, \$mw, $gui_data], )->pack(@{$gui_data->{button_pack_opts}}); $ctrl_F->Button(-text => 'Exit', -command => sub { exit })->pack; } MainLoop; { my $nmap_running; BEGIN { $nmap_running = 0 } { my ($nmap_pipe, $nmap_pid); sub start_nmap { my ($mw, $gui_data) = @_; $nmap_pid = open $nmap_pipe, '-|', 'nmap -A localhost'; $$mw->fileevent($nmap_pipe, readable => sub { if (defined(my $read = scalar <$nmap_pipe>)) { $gui_data->{out_T}->insert(end => $read); $gui_data->{out_T}->yview('end'); } else { stop_nmap($mw, $gui_data); } }); $nmap_running = 1; @$gui_data{qw{start_button_text stop_button_text}} = (get_start_button_text(), get_stop_button_text()); $gui_data->{start_B}->configure(-state => 'disabled'); $gui_data->{stop_B}->configure(-state => 'normal'); } sub stop_nmap { my ($mw, $gui_data) = @_; $$mw->fileevent($nmap_pipe, readable => ''); kill INT => $nmap_pid if kill 0 => $nmap_pid; $nmap_running = 0; @$gui_data{qw{start_button_text stop_button_text}} = (get_start_button_text(), get_stop_button_text()); $gui_data->{start_B}->configure(-state => 'normal'); $gui_data->{stop_B}->configure(-state => 'disabled'); } } { my (@start_button_texts, @stop_button_texts); BEGIN { @start_button_texts = ('Start `nmap`', '`nmap` running ... +'); @stop_button_texts = ('`nmap` not running', 'Stop `nmap`') +; } sub get_start_button_text { $start_button_texts[$nmap_running] + } sub get_stop_button_text { $stop_button_texts[$nmap_running] } sub get_button_width () { length +(sort { length $b <=> length $a } @start_button_texts, @stop_button_texts)[0]; } } }

— Ken


In reply to Re: Perl Tk nonblocking by kcott
in thread 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.