I think you'd better anchor that regex or else your /file_name/ will match a file called "this_is_a_file_named_Joe" (that is to say use /^file_name$/). Even better would be to use eq in this case (with suitable use of lc if you are using a case-insensitive file system).

A larger problem is that the directory contents could well have changed between the readdir and the testing loop, so your result won't be certain. Since you are going to die when you find a match anyway, you could just as well put the readdir in the loop condition and not bother with the array at all, which makes your test somewhat more reliable. Even so, you're still testing whether a variable (the return value of the readdir) matches a string, which does not really tell you anything certain about a file on disk.

The best solution would be to use the file test operator: die $message if -f '/ber/baz/file_name'.

Update: A fair point Tyke. Of course -e also returns true for a directory, for which one might want to be more specific using -d if one were actually looking for a directory. We don't know the greater purpose of the test in this case, but perldoc -f -X should certainly be consulted to select the best test available under the circumstances.

--
I'd like to be able to assign to an luser


In reply to Re: Re: Is the file there? by Albannach
in thread Is the file there? by Ahbeyra

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.