in reply to Re: working with 2 inputs
in thread working with 2 inputs

OK, I have tried using IO::Select. This is what I got so far. After it goes into the while loop $rh_set is empty and the select doesn't wait for output. It's got 100%cpu and loops without going into ForEach loop to DoSomething. What am I doing wrong?
$w1 = open(fw1,"pgm1|"); $w2 = open(fw2,"pgm2|"); $read_set = new IO::Select(); $read_set->add($w1); $read_set->add($w2); while (1) { ; my ($rh_set) = IO::Select->select($read_set, undef, undef, 2); print $rh_set; foreach $rh (@$rh_set) { ...DoSomething... } }

Replies are listed 'Best First'.
Re^3: working with 2 inputs
by almut (Canon) on Oct 01, 2008 at 18:41 UTC

    As has already been mentioned, select doesn't work with pipes on Windows.  So this note is just for the record.

    $w1 = open(fw1,"pgm1|"); ... $read_set->add($w1);

    open() in this case (iff a pipe is involved) returns a PID, whereas IO::Select's add() method is expecting a file handle...  In other words, you'd rather do something like this (on Unix):

    my $pid1 = open my $fh1, "pgm1|"; my $pid2 = open my $fh2, "pgm2|"; my $read_set = new IO::Select(); $read_set->add($fh1); $read_set->add($fh2); ...

    BTW, as said, the return value of open() here is the PID of the subprocess, or undef if the fork failed. It says nothing about whether the command itself (i.e. pgm1 etc.) succeeded. For this reason, you'd normally also want to check the return value when closing the file handle:

    close $fh1 or warn "subprocess failed: status=$?\n";