Why do you open the file just to close it again? You know -r can take a file name, not just a file handle, right?

Putting variables in quotes when you don't need to may bite you in future when you get to using references. But maybe you won't put variables containing references in quotes and so it is just a style issue.

But the main problem with your code is that $File::Find::name will often not be useful for opening a file (nor for checking the permissions on a file).

If you call File::Find's find() and pass it "foo" as the directory to search, then find() will chdir into the "foo" directory (by default) before doing much of any work. If you have a file "foo/bar", then your 'wanted' subroutine will be called with $_ set to "bar" and $File::Find::name set to "foo/bar".

Since your program is now cd'd into "foo", trying to open "foo/bar" would only work if you also had a "foo/foo/bar" file. So try:

sub wanted { print "hello 1\n" if -r $_; }

- tye        


In reply to Re: question on File::Find (cd) by tye
in thread question n File::Find by skyworld_chen

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.