in reply to Re: How to verify a file handle?
in thread How to verify a file handle?
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:
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.)open(FILEHANDLE, "< $file") or die "Couldn't read '$file': $!";
|
|---|