I have a shell script (testScript.sh) that prints to stdout, waits a little bit, and then prints again:
#!/bin/sh echo "before" sleep 2 echo "after" I am trying to execute this sh script from a perl script like this:
#!/usr/bin/perl -w use strict; $| = 1; # do I need this? sub runCommand($) { my ($command) = @_; open CMD, "$command |" || die "Unable to run $command: $!"; my @output = <CMD>; close CMD; print join('',@output); } runCommand("./testScript.sh"); If I run the shell script directly I immediately see "before", then a delay, and then "after".
But, when I run the script via perl I first get the delay and then "before" and "after" are printed.
I was thinking that this was because of Perl's I/O buffering, but the $| = 1 doesn't seem to be having any effect.
What am I missing here? How can execute the script via perl and still have it immediately write to stdout?
$| = 1;only makes a difference if the output of the Perl script is redirected, yet there's no mention of that.$| = 1;therefore has no effect.$| = 1;is unnecessaryunbufferutility from theexpectdistribution.