#!/usr/bin/perl use strict; use Tk; use Tk::ROText; use Tk::ProgressBar; my ($complete_steps,$steps,$progress,$pid) = (14,0,0,undef); my $mw = new MainWindow; my $button = $mw->Button(-text => 'Run', -command => sub{&run})->pack(); my $pb = $mw->ProgressBar(-variable=>\$progress,-width => 20,-blocks=>100,-gap => 0)->pack(-fill => 'x', -expand => 1); my $output = $mw->Scrolled('ROText', -scrollbars => 'osoe', '-wrap','none')->pack(-anchor => 'nw', -fill => 'both', -expand => 1); MainLoop(); sub run { my $cmd = "..."; my ($flag,$cmd_handle); $pid = open ($cmd_handle, "$cmd 2>&1 |") or $flag = $!; unless ($flag){ $mw->fileevent($cmd_handle,'readable', [\&working,$cmd_handle]); } else{} } sub working { my ($cmd_handle) = @_; my $redline; my $stat = sysread $cmd_handle, $redline, 4096; unless ($stat == 0) { $output->insert('end', "$redline");$output->yview('end'); &update_remaining($redline); } else { $mw->fileevent($cmd_handle ,'readable',''); $steps = 0; $progress = 0; } } sub update_remaining { my ($redline) = @_; my @list= ($redline =~ /Now/g); $steps += scalar(@list); my $temp = 100 * ($steps/$complete_steps); $progress = $temp; }