in reply to Printing to STDOUT buffered when invoking Perl within a bash-script using tee

Hi TJCooper,

Sounds like you are Suffering from Buffering. In your Perl script, turn on autoflush via $|=1;.

Hope this helps,
-- Hauke D

  • Comment on Re: Printing to STDOUT buffered when invoking Perl within a bash-script using tee
  • Download Code

Replies are listed 'Best First'.
Re^2: Printing to STDOUT buffered when invoking Perl within a bash-script using tee
by TJCooper (Beadle) on Dec 05, 2016 at 15:11 UTC

    I have tried this but the fact the Perl script is NOT buffered when ran as a standalone script or when tee/exec is not used within the Bash script suggests to me it's not an issue with the Perl script itself but rather it's interaction with the Bash.

    Update 1: I had tried $|=1; however the fact that NO buffering occurs when the Perl script is run as a standalone or when exec/tee is not used within the Bash script, suggests to me that it's not an issue inherent to the Perl but rather a problem with its interaction with Bash.

    Update 2: $|=1; now appears to work and solves the issue - i'm not sure why it did not before.

      Hi TJCooper,

      the Perl script is NOT buffered when ran as a standalone script

      A described in the article I linked to, Perl decides whether STDOUT should be line or block buffered depending on whether it is connected to a terminal or not. For example, you should notice a difference in the behavior of the following: the middle one delays its output until the end due to the buffering.

      $ perl -le 'for (0..10) { print; sleep 1 }' $ perl -le 'for (0..10) { print; sleep 1 }' | tee /tmp/foo $ perl -le '$|=1; for (0..10) { print; sleep 1 }' | tee /tmp/foo
      I have tried this

      In the following bash script, if I remove the $|=1;, it displays the undesired behavior you describe; with the autoflush turned on it shows the expected "live" view.

      #!/bin/bash exec > >(tee "/tmp/foo") perl -le '$|=1; for (0..10) { print; sleep 1 }'

      So if you've tried $|=1; and are still getting the undesired behavior, perhaps you could show a Short, Self Contained, Correct (Compatible) Example for us to reproduce the problem.

      ... after your update to the node while I was typing my reply:

      I had tried using that magic-variable and it did not work...but now it does.

      Please see How do I change/delete my post?, especially "It is uncool to update a node in a way that renders replies confusing or meaningless".

      Regards,
      -- Hauke D

      Update: Referenced article.