my $p1 = open (...) or die;
my $p2 = open (...) or die;
my $p3 = open (...) or die;
my $BITS = "";
vec ($BITS, fileno ($_), 1) = 1 for $p1, $p2, $p3;
while (1) {
my $rbits = $BITS;
my $ebits = $BITS;
select ($rbits, undef, $ebits, TIMEOUT);
if ($ebits) {raise_hell ()}
if (vec ($rbits, fileno ($p1), 1) == 1) {something ()}
if (vec ($rbits, fileno ($p2), 1) == 1) {something_else ()}
if (vec ($rbits, fileno ($p3), 1) == 1) {another_thing ()}
}
But this didn't behave I wanted it to. Can you spot the mistake?
select modifies its arguments (Score:2)
The first 3 arguments to
selectare value/return - on input you give a bitmask of the file descriptors you are interested in, on output only the bits corresponding "interesting" file descriptors are set. So it's likely that after the first call toselect, all the bits were zeroed, and so for subsequent calls the arguments you passed in say that you're not interested in any file descriptors, so you never call any of your action functions. The solution is somethingRe: (Score:1)
Re: (Score:2)
Ah OK. So I was right when I had this hunch on hitting "Submit" that I'd missed something. :-)
Reinventing the wheel (Score:2)
Isn't there a module for this? Your mistake is in writing your own buggy code instead of using somebody's better tested module. :P
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
Re: (Score:1)
Re: (Score:1)
Speaking of wheels, this seems an ideal candidate for a POE [cpan.org] session and POE::Wheel::FollowTail.
Re: (Score:1)
shave off lines (Reinventing the wheel) (Score:1)
Re: (Score:1)
Wrong order (Score:1)
Obviously, you should be doing
another_thing()in response to$p2coming ready. I wish you wouldn't post such trivial problems.fileno argument? (Score:1)
Can it really be that simple? (Score:1)
open [perl.org] does not return the filehandle, so it would appear that $p1 and friends are not what you seem to expect based on the rest of the code.
Re: (Score:1)
$ebits string is always true (Score:1)
one issue i see is that you don't handle when a socket/pipe closes. without removing a closed handle from that data it looks like it may do an infinite loop of reading zero bytes.
but the bug i see is the if( $ebits ) line. since $ebits is a char string and not likely be '0' or '', it will be true all the time. you need to eit