in reply to working with 2 inputs

See IO::Select, select, threads, POE::Wheel::Follow or Coro. Likely, AnyEvent also provides an abstraction of a select loop.

It really depends how much frameworkness you need. IO::Select nicely? abstracts away most of the uglyness of select, which is why I listed it before the select call itself. All other modules are frameworks that more or less tie you into their respective idiom of doing IO and other event handling. AnyEvent tries to abstract away the main event loop.

Replies are listed 'Best First'.
Re^2: working with 2 inputs
by salva (Canon) on Oct 01, 2008 at 11:09 UTC
Re^2: working with 2 inputs
by sktron (Novice) on Oct 01, 2008 at 14:48 UTC
    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... } }

      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";