in reply to Re: How to print called program to stdout
in thread How to print called program to stdout

$| =1; is a key thing.

What $| =1; does is un-buffer STDOUT. STDERR is already un-buffered by default.

What buffering does is to gather up stuff until it reaches a "quanta" of stuff to write. Then the O/S writes it all at once. This difference matters a lot in terms of performance. For writing to a HD, maybe 4Kbytes or 8Kbytes might be the "quanta" for stdout.

When $|=1 is in effect, each print statement will actually access the HD for every print.

Note that you can redirect stdout and stderr individually from the command line. The default is only re-direct stdout, "someprog >stdoutfile"."command > combinedfile 2>&1" re-directs both std-err and std-out to the same file.

For some programs that run for a long time on the command line, I use stderr to print status, "working on X..". I do that so that I can see that the program is not "hung". And I send any actual error to both stderr and stdout.

I think that all you need to do is set: $|=1; When you do that, the "errors" and the "output" will occur together on the stdout console. When you have the program working, then take $|=1; out if you want it to run faster. $|=1 eliminates the delay between an error and the next stdout line, also stdout lines appear in "real time" instead of there being a delay. It is certainly possible that if the "buffering write quanta" is not fulfilled, that the program doesn't print anything until it actually has already exited!

  • Comment on Re^2: How to print called program to stdout

Replies are listed 'Best First'.
Re^3: How to print called program to stdout
by Anonymous Monk on Mar 19, 2016 at 14:58 UTC
    I only use that on web servers. I want it to print back to the output so I can see the results in the browser. I never use it on my local machine.

      Then how does your "main program" call the "subprogram" during the test -- how is your test script interfacing with the webserver? Is the "main program" also on the webserver, and if so, is it calling the "subprogram" as a function call, or thru a system() or `` access, or something else? Or is the "main program" on your local machine, accesssing the webserver "subprogram" via LWP::Simple? Or something else entirely?

      Since it's printing to STDOUT to the web browser, are you sure you've not accidentally embedded the "subprogram" output inside something hidden in the HTML? Could it be inside a comment, accidentally inside a tag, or in a non-displayed element? (view-source is your friend when a CGI's output is not where you think it should be).