in reply to How to verify a file handle?

I think the normal way to do this is by making the script die if the file that the filehandle is... er... attached to (I have no clue what the proper word is either, but I know what you mean) isn't there. So
open (FILEHANDLE, "$file") or die "couldn't open $file";
Certainly, that's what everyone tells me to do when I don't do it :)

§ George Sherston

Replies are listed 'Best First'.
Re (tilly) 2 (wrong question): How to verify a file handle?
by tilly (Archbishop) on Oct 02, 2001 at 21:19 UTC
    His question asked how to verify that something you were just passed is really a filehandle and not, say, a string.

    You are talking about how to open up a filehandle based on having the name of a file in a string.

    Incidentally your snippet is not how you are supposed to do it. As it says in perlstyle, you need to include the output of $! in the error message. The information on what the OS thinks is wrong is often very important in debugging. Furthermore if you are accepting a filename from user input, and you do not trust the user (this is common in CGI scripts) then you do not want to open a file for reading that way. Instead you want to:

    open(FILEHANDLE, "< $file") or die "Couldn't read '$file': $!";
    The difference being that now you are only willing to read the file. So if they send you "filenames" which contain cute characters like "|", you won't gratify them by running arbitrary programs. (But you still need to worry about them naming files you don't want named. But that is a more complex issue by far.)