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

So I am launching some perl scripts from a Win32 application that I wrote. I am redirecting the stderr and stdout filehandles of the perl script via my Win32 application.

What I need to do in order for the IPC(using pipes) to work is disable buffering. However, I only want to disable buffering if I am launching the script from the Win32 application.

At the top of my perl script I select STDERR as teh default filehandle and I make it hot($| = 1). Since in my scripts I sometimes write to STDOUT I say print STDOUT "hello world\n". I need to make the STDOUT filehandle hot and I have to set STDOUT as the default filehandle so STDERR will no longer be hot. What's the easiest way to get this done? TIA.

Replies are listed 'Best First'.
Re: Disabling Buffering
by ikegami (Patriarch) on Nov 29, 2004 at 21:31 UTC

    Neither STDERR nor STDOUT will be buffered (they'll be "hot") after these lines:

    select(STDERR); $|=1; # Autoflush STDERR. select(STDOUT); # This doesn't undo STDERR's autoflushing. $|=1; # Autoflush STDOUT. # STDOUT will be the default print handle # because it was the last select()ed handle.
      BTW, STDERR is hot by default. No code is required to turn that on.
Re: Disabling Buffering
by zejames (Hermit) on Nov 29, 2004 at 21:35 UTC
    Another Way to Write It :
    use IO::Handle; STDOUT->autoflush(1); STDERR->autoflush(1);

    --
    zejames
Re: Disabling Buffering
by Fletch (Bishop) on Nov 29, 2004 at 20:44 UTC

    The surest, simplest way would be to just pass an argument (--nobuffer) or set something in the environement from your launching program, then check for that inside your perl (either by parsing @ARGV or looking at $ENV{HEYDONOTBUFFER} or what not).

      Fletch, that's not the question I am asking an answer to.

      What I want is a clean way of making both STDERR and STDOUT hot for the entire perl script. I just want to add code at the top of the script so that when I do my check to see if I want buffering I can add code to make the filehandles hot.

      This is what I want ideally if possible:

      if(want to buffer) { ..... something to make STDERR and STDOUT hot for entire script .... }

      TIA.

        So do it. If you set autoflush on a handle it'll stay unbuffered until something else sets it back. Unless you have code that's twiddling $| or doing STDOUT->autoflush( 0 ) they should stay that way.

Re: Disabling Buffering
by bmc_mark (Novice) on Nov 29, 2004 at 21:36 UTC
    I sent my question to fletch, I meant richz, sorry..
      Ignore the previous post, sorry I'm new here. Richz, I have been looking for a way to capture stdout/stderr output from a process called with Win32 Process Create..looks like you have that handled. Can you post the snippet for that, please? I'm pretty desperate... Thanks, Mark

        Are you sure you need to start the other process by Win32::Process? Would not IPC::Open3 work better?

        Jenda
        We'd like to help you learn to help yourself
        Look around you, all you see are sympathetic eyes
        Stroll around the grounds until you feel at home
           -- P. Simon in Mrs. Robinson

        bmc_mark, Not that it really matters but is the process you are creating a perl script?
      Yeah, I am getting some weirdness in my Win32 app. I guess the I/O redirection isn't working flawlessly; that's somewhat strange since my other 2 perl scripts run great from my Win32 app w/o disabling buffering...
        Rich, it's a .exe that is run from the command line, all text output. Output goes to cmd shell...