in reply to Help me update stubborn perlfaq answers!

For the close, I would definitely recommend
use POSIX; POSIX::close($fd) or die "close failed: $!";
over the syscall.

And perhaps add a comment about the portability of using numeric file descriptors, but I'm not qualified to write such a comment.

Replies are listed 'Best First'.
Re^2: Help me update stubborn perlfaq answers!
by brian_d_foy (Abbot) on Apr 18, 2005 at 20:15 UTC

    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>
      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) );
      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.