Hello everybody.

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

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.