First, I find using threads easier than a fork and pipe. When I run your code on linux, I get an error when I press the fork button.
perl: xcb_io.c:183: process_responses: Assertion `((int) (((dpy->last_request_read)) - ((dpy->request))) <= 0)' failed. Aborted
Probably because the pipes don't exist?

This is how I would do it with a thread, there are many other ways. It waits for full output, thru the backtick.

#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; my $ret:shared = ''; my $go:shared = 0; my $die:shared = 0; my $val = 0; #create thread before any tk code is called my $thr = threads->create( \&worker ); use Tk; my $mw = MainWindow->new(); $mw->protocol('WM_DELETE_WINDOW' => sub { &clean_exit }); my $canvas = $mw->Scrolled('Canvas', -bg =>'white', -scrollregion=>[0,0,1000,1000] )->pack(-expand=>1,-fill=>'both'); my $text; my $button1 = $mw->Button( -text => 'Start', -command => sub{ $canvas->itemconfigure($text,-text=> ''); $canvas->update; $go = 1; }, )->pack(); my $button2 = $mw->Button( -text => 'Stop thread', -command => sub{ $die = 1; $thr->join; }, )->pack(); my $timer = $mw->repeat(10,sub{ if($ret ne ''){ $text = $canvas->createText(20,10, -anchor=>'nw', -fill => 'black', -text => $ret, ); $canvas->configure(-scrollregion => [$canvas->bbox('all') +]); $ret = ''; } }); MainLoop; sub clean_exit{ $timer->cancel; my @running_threads = threads->list; if (scalar(@running_threads) < 1){print "\nFinished\n";exit} else{ $die = 1; $thr->join; exit; } } # no Tk code in thread sub worker { while(1){ if($die){return} if($go){ $ret = `ls -la`; # print "$ret\n"; $go = 0; } # print 1; select(undef,undef,undef,.1); } }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

In reply to Re: Tk and using a pipe to read command results by zentara
in thread Tk and using a pipe to read command results by drake50

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.