fileno 
fileno FILEHANDLEThis function returns the file descriptor underlying a
filehandle. If the filehandle is not open, fileno returns undef. If there is no real file descriptor at
the OS level, as can happen with filehandles connected to memory objects
via open with a reference for the
third argument, −1 is
returned.
A file descriptor
is a small, nonnegative integer like 0 or 1, in contrast to
filehandles like STDIN and STDOUT, which are symbols. Unfortunately, the
operating system doesn’t know about your cool symbols. It only thinks of
open files using these small file numbers, and although Perl will
usually do the translations for you automatically, occasionally you have
to know the actual file descriptor.
So, for example, the fileno
function is useful for constructing bitmaps for select and for passing to certain obscure
system calls if syscall(2) is implemented. It’s
also useful for double checking that the open function gave you the file descriptor you
wanted and for determining whether two filehandles use the same system
file descriptor.
if (fileno(THIS) == fileno(THAT)) {
say "THIS and THAT are dups";
}If FILEHANDLE is an expression, the
value is taken as an indirect filehandle, generally its name or a
reference to something resembling a filehandle object.
Don’t count on the association of a Perl filehandle and a numeric file descriptor throughout the life of the program. If ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access