I am writing code for a TK GUI, from which I am running a shell command and display its output on the window. I have also included a progressbar. The backbone of my code is shown below
#!/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','n +one')->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; }
The problem is that while everything works ok and nothing blocks, perl seems to accepts the data from the pipe in batches (around 4000 characters at a time). When running the command directly in the shell, the output is gradually shown (line by line). I assume that "somewhere" between the command and the output window there is a buffer that I cannot locate and this messes up the progress bar smoothness.
I have checked that the number of bytes I use in the sysread doesn't play a role in that. So, any help is welcome.
Thanks in advance for your time.
In reply to Reading from a command pipe by DimosTsag
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |