[comp.lang.perl] What do you do after you've done select

okamoto@hpcc01.HP.COM (Jeff Okamoto) (04/25/91)

I have a question about the select(2) emulation in perl.

There are plenty of places that explain how to set up for the call to
select, using fileno and vec to build the bitmap.  What those same examples
DON'T provide is a good way to determine which file handle(s) are ready for
each operation.

One way that I have seen, after going back through the archives is to keep
an associative array with the names of the file handles and their fileno()
values.  What I am wondering is, is there a better way?

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (05/04/91)

In article <1180015@hpcc01.HP.COM> okamoto@hpcc01.HP.COM (Jeff Okamoto) writes:
: I have a question about the select(2) emulation in perl.
: 
: There are plenty of places that explain how to set up for the call to
: select, using fileno and vec to build the bitmap.  What those same examples
: DON'T provide is a good way to determine which file handle(s) are ready for
: each operation.
: 
: One way that I have seen, after going back through the archives is to keep
: an associative array with the names of the file handles and their fileno()
: values.  What I am wondering is, is there a better way?

It depends on how many filehandles you're managing.  For a few filehandles,
polling the vector from an associative array is probably reasonable.
For lots of filehandles, I'd make a back-translation table:

	$handle[fileno(STDOUT)] = 'STDOUT';

and then I'd unpack the vector and search it:

	$nfound = select($rout = $rin, $wout = $win, $xout = $xin, $timeout);
	$bits = unpack('b256', $rout);
	for ($fd = index($bits,'1'); $fd >= 0; $fd = index($bits,'1',$fd+1)) {
	    $handle = $handle[$fd];
	    if (defined $handle) {
		# do input using indirect filehandle or type glob
	    }
	}

and similarly for $wout and $xout.

Larry