Tk::ExecuteCommand - execute a command asynchronously (non-blocking).


 $exec = $parent->ExecuteCommand;


Tk::ExecuteCommand runs a command yet still allows Tk events to flow. All command output and errors are displayed in a window.

This ExecuteCommand mega widget is composed of an LabEntry widget for command entry, a "Do It" Button that initiates command execution, and a ROText widget that collects command execution output.

While the command is executing, the "Do It" Button changes to a "Cancel" Button that can prematurely kill the executing command. The kill_command method does the same thing programmatically.

The primary benefit of this widget is the ability to execute system commands asynchronously without blocking Tk's event loop. The widget doesn't even have to be managed (pack/grid), see the EXAMPLES section.



The command to execute asynchronously.


Character width of command Entry widget.


Character height of the ROText widget.


Label text for command Entry widget.


Label text for "Do It!" Button.


Character width of the ROText widget.



Initiates command execution.


Returns a 2 element array of $? and $! from last command execution.


Terminates the command. This subroutine is called automatically via an OnDestroy handler when the ExecuteCommand widget goes away.


packForgets all but the minimal ROText widget. Currently, this action cannot be rescinded.


Component subwidgets can be accessed via the Subwidget method. Valid subwidget names are listed below.

Name: command, Class: LabEntry

Refers to the command LabEntry widget.

Name: doit, Class: Button

Refers to the command execution Button.

Name: spacer, Class: Frame

Refers to the spacer Frame separating the Entry and ROText widgets.

Name: label, Class: Label

Refers to the Label across the top of the ROText widget.

Name: text, Class: ROText

Refers to the ROText widget that collects command execution output.


 $ec = $mw->ExecuteCommand(
     -command    => '',
     -entryWidth => 50,
     -height     => 10,
     -label      => '',
     -text       => 'Execute',
 $ec->configure(-command => 'mtx -f /dev/sch0 load 1 0');


 # More complicated example to read AC temps via snmpget. The target
 # air conditioner IPs have been changed to protect them ;)

 use Tk;
 use Tk::ExecuteCommand;
 use subs qw/ init main read_acs sys /;
 use strict;
 use warnings;

 # Globals.

 my $ec;                                 # ExecuteCommand widget
 my @gauges;                             # list of AC NGauge widgets
 my $interval;                           # interval between SNMP scans, seconds
 my $mw;                                 # MainWindow
 my $snmp_liebert_temperature_actual;    # temperature, actual reading
 my $snmp_liebert_temperature_tolerance; # temperature, desired tolerance
 my $snmp_liebert_temperature_setting;   # temperature, desired setting
 my $snmp_root;                          # snmpget/snmpset dirname
 my $temp_tolerance_factor;              # tolerance value * factor = degrees


 sub init {

     $mw = MainWindow->new;
     $ec = $mw->ExecuteCommand;

     $interval = 2;

     $snmp_root = '/usr/bin';
     $snmp_liebert_temperature_setting   = '.';
     $snmp_liebert_temperature_tolerance = '.';
     $snmp_liebert_temperature_actual    = '.';

     $gauges[0] = {-ac => 'some-ip-1'};
     $gauges[1] = {-ac => 'some-ip-2'};

 } # end init

 sub main {


 } # end main

 sub read_acs {

     my( $stat, @temperature, @humidity );

     foreach my $g ( @gauges ) {
         my $ac_ip = $g->{ -ac } . '';
         ( $stat, @temperature ) = sys "$snmp_root/snmpget $ac_ip communityname  $snmp_liebert_temperature_setting $snmp_liebert_temperature_tolerance $snmp_liebert_temperature_actual";
         die "Cannot get temperature data for AC '$ac_ip': $stat." if $stat or $#temperature != 2;
         print "stat=$stat, data=@temperature.\n";

     } # forend all air conditioners

     $mw->after( $interval * 1000 => \&read_acs );

 } # end read_acs

 sub sys {

     # Execute a command asynchronously and return its status and output.

     my $cmd = shift;
     $ec->configure( -command => $cmd );
     my $t = $ec->Subwidget( 'text' ); # ROText widget
     $t->delete( '1.0' => 'end' );
     return ($ec->get_status)[0], split /\n/, $t->get( '1.0' => 'end -1 chars' );

 } # end sys


exec, command, fork, asynchronous, non-blocking, widget


Copyright (C) 1999 - 2004 Stephen O. Lidie. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.