Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am getting an error message (select: Bad file descriptor) when attempting to use a bit vector (formed with vec) and select to scan for open file descriptors/network connections to be read from.

If I did the following, it doesn't complain:
$fdvec = '';
Adding a file descriptor to the bit vector:
vec($fdvec, $fh->fileno, 1) = 1;
And selecting/polling for ready descriptors:
$ready = select ($rout = $fdvec, undef, undef, $timeout);
If I convert all of the above to a hash ref, i.e.
$fdvec ====> $this->{fdvec}
Then I'm getting the error message. Is there something about using the hash ref with vec or select that is an issue?

Replies are listed 'Best First'.
Re: Using vec/select with a hash ref
by ikegami (Patriarch) on Feb 03, 2009 at 23:52 UTC

    Please add <p> in front of every paragraph and place code in <c>...</c> tags.

    What gives the error? You haven't shown that.

    You'll have a much easier time using IO::Select. It's a thin wrapper around select that handles the bit vector for you.

Re: Using vec/select with a hash ref
by targetsmart (Curate) on Feb 04, 2009 at 06:18 UTC
    The best solution is to use IO::Select, but a temporary solution to your problem is; before doing anything on the file handle(like open/read/write etc) just check whether it is alive by using fileno or eof functions, so that you can atleast avoid this 'bad file descriptor error'.
    Just show the piece of code to give you clear directions.
    This http://www.perlmonks.org/?node_id=740330 might also help you.
    UPDATE
    I think that you are checking with $fdvec contents after select returns, if you are doing so, it is wrong; you have to check the contents of $rout. man perlipc may also help you.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.