I tried to send off-list, but the e-mail address did not
work, so I'm posting to the list:



Hey, I was very impressed when this script you provided
ran for me.  All I did is run the first script and it
creates the box nicely.

I don't know much about spawning child processes.  I'm
on vacation and much of my Perl documentation is at
home.

I was wondering if you could help me get this script
tied to one of my Perl programs on my Win2000 box.
After much experimentation, I've changed the line
open(C, "./read-own-stdout-piper 2>&1 |") or warn $!;
to
open(C, "round2.bat 2>&1 |") or warn $!;

round2.bat is a DOS batch file with a single line:
perl round2.pl    # (that is the way I run perl programs)

round2.pl has:

use strict;
use warnings;
my $number = 5.6278;
my $rounded = sprintf '%.2f', $number;
print "$rounded\n";
$|++;                           # << SHOULD THIS LINE BE HERE?

AND IT ALL WORKS!


I can't just do
open(C, "round2.pl 2>&1 |") or warn $!;
because then it just opens round2.pl in my text editor,
since that is the association I have ".pl" set to.  I tried
open(C, "'perl round2.pl' 2>&1 |") or warn $!;
but that didn't work either.

That "Got:" prompt isn't too cool,  but I'm sure I'll
learn how to turn that off later.

Also, it's unfortunate that copy and paste don't work
in that TK box.

Anyway, thanks a bunch.  If there are other posts
on the NG related to this, I'll see them when I get
home.


Mike Flannigan
Houston, TX


_______________________________________


Subject:  Re: Regarding Text Widget in Perl/Tk
   Date:  Fri, 30 Jan 2004 10:22:24 -0500
   From:  zentara <[EMAIL PROTECTED]>
     To:  [EMAIL PROTECTED]


On Thu, 29 Jan 2004 20:01:10 +0530, [EMAIL PROTECTED] (Vinesh
Varghese) wrote:

>I am presently working on an Automation project where I am using Active

>state perl as the programming language on windows platform. For the
>above mentioned project I am using Perl/Tk for the GUI. I created a
Text
>Widget and wanted to  show the output from my perl code on the text
box.
>I tried tieing the STDOUT to the text box , but the output is shown
only
>after the program is terminated. What ever I am printing to STDOUT is
>accumulated and shown at once after the program is terminated. I wanted

>to show them as and when the program is running. I don't know where I
am
>mistaken. Please help me in this regard.

Since you don't show your code, it's hard to say what your problem is.

Here is a set of programs, that do what you want.
######################################################
#!/usr/bin/perl -w
use strict;
use Tk;
my $mw = new MainWindow;

my $listbox = $mw->Scrolled("Listbox", -scrollbars => "osoe",
    -height => 5,
    -selectmode => "multiple")
    ->pack(-side => "left");

my $start = $mw->Button( -text => 'Start', -command => \&start )->pack;
$mw->Button( -text => 'Exit',  -command => \&exit )->pack;


#start external command to pipe
sub start {
    $start->configure(-state => 'disabled');
    open(C, "./read-own-stdout-piper 2>&1 |") or warn $!;
$mw->fileevent( \*C, 'readable', \&doSomething );

}

sub doSomething {
    if ( eof(C) ) {    # Child closed pipe
          close(C);      # Close parent's part of pipe,
                        # filevent is cancelled as well
          wait;          # Avoid zombies
    return;
    }
    my $text = <C>;    # Get text from child and put it into listbox
    chomp($text);
    $listbox->insert( 'end', 'Got: ' . $text );
    $listbox->see('end');
}

MainLoop;

__END__
##############################################
#and here is read-own-stdout-piper

#!/usr/bin/perl
$|++;
for my $i ( 0 .. 10) {
        print $i, "\n";
        sleep 1;
    }
__END__
############################################



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to