Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

E.g.
- $stream1 is the STDOUT of a child process and $stream2 is the STDERR of the same child process
- $stream1 is one child process and $stream2 is another child process

Is there a possiblility in perl to monitor two or more streams at the same time?

So we loop as long as anything pops-up in any of the streams and we take whatever comes first either in $stream1 or $stream2.

Something like

while (my $line = <$stream1 or $stream2> ) {  
    #do something with the $line
}

??

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Sure there is - it's Perl, after all - just not with quite such an elegant syntax.

Monitoring several streams boils down to judicious use of the select syscall, and in Perl that means using the four-argument version of select, which wraps this syscall. Many people find this a bit too cumbersome and low-level, so of course there is a module which further wraps the pattern for your convenience.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.