schweini has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,
In this perl/Tk program i'm working on, i am opening a pipe to a simplistic, minimalistic "print-preview" Tk program:
open(VIRTUALPRN, "| perl print.pl");
... and some thime later i simply do some
print VIRTUALPRN "foobar\n"
but the "preview" program only pops up after i close VIRTUALPRN (something which i don't want to do), and my main program waits for the preview program to terminate before ending (something i don't want, either).
this is under WinXP, BTW.

Anybody know how i can make the previewer pop up immediatly after the open(), let my main program not wait for the previewer to exit, and make all of this DWIMANWIS (Do What I Mean, And Not What I Say)?

here's the previewer:
use Tk; $mw = MainWindow->new(-width => 300, -height => 600); $pp = $mw->Scrolled("Text")->pack; $pp->configure( -background => 'white', -scrollbars => 'osoe', -font => '*-courier-medium-r-*-*-17-*' ); tie(*PP, 'Tk::Text', $pp); sub foo { while($l = <STDIN>) { print INVOICEVIEW $l; $pp->update(); } } MainLoop();

Replies are listed 'Best First'.
Re: Pipeing to a Tk-App
by graff (Chancellor) on Feb 17, 2003 at 05:42 UTC
    After you do this:
    open(VIRTUALPRN, "| perl print.pl");
    ... and before you do this:
    print VIRTUALPRN "foobar\n"
    you could try this:
    select VIRTUALPRN; # make this the "default" output handle $|++; # disable output buffering on this handle
    Or, more elegantly:
    use FileHandle; ... my $VirtualPrnFH = new FileHandle; $VirtualPrnFH->open( "| perl print.pl" ); $VirtualPrnFH->autoflush(1); # turn off buffering ... print $VirtualPrnFH "foobar\n"; ...
    If you're only writing a small amount of content to that process, the default buffering would withhold output to the process until the file handle closes, which you could do explicitely in the parent process (rather than just exiting), but it's better to turn off buffering.
      yep.
      that did the trick. i forgot to select VIRTUALPRN before doing the $|++.
      silly me.
      Thanks a lot!