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.)

In reply to Re (tilly) 2 (wrong question): How to verify a file handle? by tilly
in thread How to verify a file handle? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.