7.19. Opening and Closing File Descriptors by NumberProblemYou know which file descriptors you'd like to do I/O on, but Perl requires filehandles, not descriptor numbers. Solution
To open the file descriptor, use the open(FH, "<&=$FDNUM"); # open FH to the descriptor itself open(FH, "<&$FDNUM"); # open FH to a copy of the descriptor use IO::Handle; $fh->fdopen($FDNUM, "r"); # open file descriptor 3 for reading
To close one by number, either use the Discussion
Occasionally you have a file descriptor but no filehandle. Perl's I/O system uses filehandles instead of file descriptors, so you have to make a new filehandle for an already open file descriptor. The If you have version 5.004 or better installed, you can use the IO::Handle object method. This is the same as: use IO::Handle; $fh = IO::Handle->new(); $fh->fdopen(3, "r"); # open fd 3 for reading
Closing a file descriptor by number is even rarer. The
Here's how you'd open the file descriptors that the MH mail system feeds its child processes. It identifies them in the environment variable $fd = $ENV{MHCONTEXTFD}; open(MHCONTEXT, "<&=$fd") or die "couldn't fdopen $fd: $!"; # after processing close(MHCONTEXT) or die "couldn't close context file: $!";
If you want to close a descriptor by number, just See Also
The Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|