in reply to Re: Help me update stubborn perlfaq answers!
in thread Help me update stubborn perlfaq answers!

Good deal: I'll add that to the answer.

Can anyone enlighten me about portability of numeric file descriptors? Does anyone use numeric fds for anything? I've used them on the command line, but that's it.

--
brian d foy <brian@stonehenge.com>
  • Comment on Re^2: Help me update stubborn perlfaq answers!

Replies are listed 'Best First'.
Re^3: Help me update stubborn perlfaq answers!
by darrellb (Acolyte) on Apr 18, 2005 at 21:05 UTC
    Ditto on the POSIX::close. I don't know about the portability of numeric file descriptors, but I recently used them.

    I needed to test a method that uses perl's syswrite on a socket, so my first thought was "pipe." But perl's pipe() threatened me with "Perl's pipes use IO buffering..." so I used POSIX::pipe instead which returns numeric fds. But syswrite wants filehandles. So I did this:

    my ($read_fd, $write_fd) = POSIX::pipe(); open(my $read_fh, "<&=$read_fd") or die "fdopen for read: $!"; open(my $write_fh, ">&=$write_fd") or die "fdopen for write: $!"; $write_fh->blocking(0); ... $client->{sock} = $write_fh; ... $client->_syswrite($topic, $msg) ... ok( sysread($read_fh, $buf, length($msg)), length($msg) );
Re^3: Help me update stubborn perlfaq answers!
by darrellb (Acolyte) on Apr 18, 2005 at 23:57 UTC
    It occurs to me that another common usage of numeric file descriptors is when constructing the bit vectors for select (the RBITS,WBITS,EBITS,TIMEOUT version, not the FILEHANDLE one). You often see something like vec($rin,fileno(STDIN),1) = 1;, where you wouldn't need the fileno() if you already had a numeric file descriptor.