in reply to Tk with threads

With Tk, you MUST create the thread before any Tk objects are created, because duplicate copies of them will get cross-linked in the threads, and cause weird errors. Here is the simplest solution. Create your threads first, and put them in a sleep loop. Then build your Tk frontend and control the threads thru shared variables. Also, do not attempt to directly modify a Tk widget from another thread. Use shared variables to signal changes, then let the main Tk thread adjust the widgets. See Tk-with-worker-threads and PerlTk on a thread.... You can also share filehandles between threads, here is a simple example (without Tk, but you could display the output in a text widget if desired)
#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; my %shash; #share(%shash); #will work only for first level keys my %hash; share ($shash{'go'}); share ($shash{'fileno'}); share ($shash{'pid'}); share ($shash{'die'}); $shash{'go'} = 0; $shash{'fileno'} = -1; $shash{'pid'} = -1; $shash{'die'} = 0; $hash{'thread'} = threads->new(\&work); $shash{'go'} = 1; sleep 1; # cheap hack to allow thread to setup my $fileno = $shash{'fileno'}; open (my $fh, "<&=$fileno") or warn "$!\n"; while ( <$fh> ){ print "In main-> $_"; } #wait for keypress to keep main thread alive <>; # control-c to exit ##################################### sub work{ $|++; while(1){ if($shash{'die'} == 1){ goto END }; if ( $shash{'go'} == 1 ){ my $pid = open(FH, "top -b |" ) or warn "$!\n"; my $fileno = fileno(FH); print "fileno->$fileno\n"; $shash{'fileno'} = $fileno; $shash{'go'} = 0; #turn off self before returning }else { sleep 1 } } END: } #####################################

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