in reply to How to verify a file handle?

To test if something is a filehandle, use the fileno() function, like this:
if (defined(fileno($fh))) { print "Filehandle!\n"; } else { print "Not a filehandle\n"; }
fileno() works as you'd expect on both bare filehandles (eg, STDERR) and on references (eg, those returns from CGI::param and CGI::upload).

Trap for the unwary: Zero is a perfectly legal fileno, which is usually bound to STDIN. Hence you should always check to make sure that fileno returns a defined value, not just a true one. Otherwise you can get caught out if you're testing STDIN for filehandle-ness, or if your STDIN has been closed and something else has taken fileno 0 as its own.

Cheers,
Paul

Replies are listed 'Best First'.
Re (tilly) 2 (no to fileno): How to verify a file handle?
by tilly (Archbishop) on Oct 02, 2001 at 21:00 UTC
    But fileno fails to work on tied handles because there is no C-level filenumber associated with it. So unless you actually need to access the handle at the C level, I would recommend not causing gratuitous breakage with calls to fileno.