Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^3: Synchronizing STDERR and STDOUT

by nothingmuch (Priest)
on Sep 21, 2006 at 11:10 UTC ( #574100=note: print w/replies, xml ) Need Help??

in reply to Re^2: Synchronizing STDERR and STDOUT
in thread Synchronizing STDERR and STDOUT

You can split the fork and the exec up if open mashes them together too much.
pipe CHILDREAD, CHILWRITE; defined( my $pid = fork ) or die "fork: $!"; if ( $pid ) { # read on CHILDREAD; } else { open STDERR, ">&CHILDWRITE"; open STDOUT, ">&CHILDWRITE"; exec( $somecmd ); }
This is precisely the type of plumbing that a shell will do when you say 2>&1, except without the unportable syntax ;-)

That said, IPC::Run and friends already abstract all of this out, so there's no need to reinvent the wheel.

zz zZ Z Z #!perl

Replies are listed 'Best First'.
Re^4: Synchronizing STDERR and STDOUT
by Ovid (Cardinal) on Sep 21, 2006 at 11:12 UTC

    Forking isn't portable. Windows only emulates it and it doesn't always do this well.

    Update: Also, I don't believe this guarantees that the streams will remain in synch. I've had plenty of problems with Test::Builder output getting corrupted when Test::Harness spits it out and if they must be completely in synch or my code fails.


    New address of my CGI Course.

      You missed my point.

      The shell will simply do a dup of 2>&1 like i illustrated. The problem is not in the fork, etc, but in that the sugar layer to do this dup, namely 2>&1 involves reparsing the command line, etc, and is thus undesirable and unportable.

      Instead of staying one level above backticks and shell redirects, you can simply do what they do.

      As for fork vs no fork - look at how IPC::Run does it. It's portable to windows and supports redirects.

      WRT synching - i am not sure (this may be impl dependant) but if there are two separate buffers for the predupped handled then they won't be in synch unless you auto flush in the child. If two dupped file descriptors will share a buffer then they will be completely synchronized, and in either case this is not going to be any different than what 2>&1 will give you.

      zz zZ Z Z #!perl

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://574100]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-12-01 05:56 GMT
Find Nodes?
    Voting Booth?