FAQ
Howdy list :)

I'm having a bit of a time with combining 2 read filehandles.

Let me explain,


use strict;
use warnings;
use IPC::Open3;

my ($write_fh, $read_fh, $error_fh);
open3($write_fh, $read_fh, $error_fh, ...

while(<$read_fh>) {
...
}

while(<$error_fh>) {
...
}

works fine, great..

What I'd like to do is process $read_fh and $error_fh in the order they
actually happen ( which is what open3($write_fh, $read_fh, $read_fh, ...
does ) but still have the error handle available afterward to look upon
specifically.

use strict;
use warnings;
use IPC::Open3;

my ($write_fh, $read_fh, $error_fh);
open3($write_fh, $read_fh, $error_fh, ...

while(magically_read_both_in_order_that_the_command_puts_them_out($read_fh,
$error_fh)) {
...
}

while(<$error_fh>) {
...
}


So that the first while loop will see:

blah 1
blah 2
oops
blah 3
blah 4
blah 5
oops again

and the second will see

oops
oops again

and not like the first example which will be:

blah 1
blah 2
blah 3
blah 4
blah 5

then

oops
oops again

Any ideas or pointers on it would be great, my head is about to explode :)

TIA!

Search Discussions

  • M. Kristall at Apr 17, 2006 at 11:59 pm

    JupiterHost.Net wrote:
    Howdy list :)

    I'm having a bit of a time with combining 2 read filehandles. [snip]
    What I'd like to do is process $read_fh and $error_fh in the order they
    actually happen ( which is what open3($write_fh, $read_fh, $read_fh, ...
    does ) but still have the error handle available afterward to look upon
    specifically.
    I didn't test this so don't assume it will work, but I think it should ;-)

    eof should return true if there is nothing left to read. So, instead of
    using <> which will block until there's something to read (or the fh is
    closed), you could periodically test eof on your file handles. Perhaps
    something like:

    sub eof_and_read {
    if (eof ($_[0])) {
    return '';
    } else {
    return <$_[0]>;
    }
    }


    Simply replacing while (<blah>) with while (eof_and_read(blah)) won't
    work too well though; you'd have to check if the file is open. The way
    to do that (that I have found) is to tell, which will return -1 if the
    fh doesn't really exist or is closed. (I recommend adapting this to
    avoid warnings where you know there will be warnings, such as tell
    ($fakefh).)

    e.g.,
    sub isopen {
    no warnings; # tell() on unopened filehandle
    return tell (shift) > -1;
    }
  • JupiterHost.Net at Apr 19, 2006 at 10:56 pm

    I didn't test this so don't assume it will work, but I think it should ;-)

    eof should return true if there is nothing left to read. So, instead of
    using <> which will block until there's something to read (or the fh is
    closed), you could periodically test eof on your file handles. Perhaps
    something like:

    sub eof_and_read {
    if (eof ($_[0])) {
    return '';
    } else {
    return <$_[0]>;
    }
    }


    Simply replacing while (<blah>) with while (eof_and_read(blah)) won't
    work too well though; you'd have to check if the file is open. The way
    to do that (that I have found) is to tell, which will return -1 if the
    fh doesn't really exist or is closed. (I recommend adapting this to
    avoid warnings where you know there will be warnings, such as tell
    ($fakefh).)

    e.g.,
    sub isopen {
    no warnings; # tell() on unopened filehandle
    return tell (shift) > -1;
    }

    I ended up using IO::Select to process the open3 read and error handles
    at the same time and in order (pretty much anyway) but still have them
    in seperate handles :)

    Thanks for your input!
  • John W. Krahn at Apr 18, 2006 at 12:22 am

    JupiterHost.Net wrote:
    Howdy list :) Hello,
    I'm having a bit of a time with combining 2 read filehandles.
    The FAQ has a good explanation about that:

    perldoc -q stderr


    John
    --
    use Perl;
    program
    fulfillment
  • JupiterHost.Net at Apr 19, 2006 at 10:54 pm

    John W. Krahn wrote:

    JupiterHost.Net wrote:
    Howdy list :)

    Hello,

    I'm having a bit of a time with combining 2 read filehandles.

    The FAQ has a good explanation about that:

    perldoc -q stderr
    That was informative, thanks!

    I ended up using IO::Select to process the open3 read and error handles
    at the same time and in order (pretty much anyway) but still have them
    in seperate handles:)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 17, '06 at 11:33p
activeApr 19, '06 at 10:56p
posts5
users3
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase