Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Perl Tk nonblocking

by kcott (Archbishop)
on Dec 01, 2016 at 05:41 UTC ( [id://1177009]=note: print w/replies, xml ) Need Help??


in reply to Perl Tk nonblocking

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:

  • Provides both a Go and a Stop button.
  • Disables the Go when running.
  • Disables the Stop when not running.
  • Changes the text on both those buttons providing primitive status feedback.

[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

Replies are listed 'Best First'.
Re^2: Perl Tk nonblocking
by rjbuckley (Novice) on Dec 01, 2016 at 09:12 UTC

    Thanks Ken, your solution works.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1177009]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-03-29 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found