larsen has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I was trying some experiments with File::Find. I found that this code does not work properly, showing only sizes of files in the current directory.
#!/usr/bin/perl use strict; use File::Find; find \&wanted, "./"; sub wanted { print "$File::Find::name ", (-s $File::Find::name); print "\n"; }

But if I write find \&wanted, "/home/stefano";, it works.
Thank you in advance.

Replies are listed 'Best First'.
Re: Problems with File::Find
by merlyn (Sage) on Aug 15, 2000 at 15:39 UTC
    Try starting at "." rather than "./". Also, File::Find doesn't follow symlinks, so if the only things that lead outward are symlinks, you won't get much recursion going on.

    -- Randal L. Schwartz, Perl hacker

      Thank you. I tried, but it doesn't work.
      Now I'm going to examine the source, to known what happens exactly.
      See you
Buzzcutbuddha (Some Corrections To File::Find) - Re: Problems with File::Find
by buzzcutbuddha (Chaplain) on Aug 15, 2000 at 16:09 UTC
    Just for clarity's sake, you might as well put parentheses around your call to find, and enclose your directory name in single quotes instead of double.

    And if you need to follow symlinks, your call to find should look like:
    find({wanted => \&wanted, follow => 1}, '.');
    This is expensive though because it creates a hash for each file. When using follow you can also use $File::Find::fullname to get the absolute pathname for the files resolved from the symlink.
    hth.
      But remember that follow doesn't exist until 5.6. If you're on 5.5 like most sane real-world machines (waiting for 5.6.1) then you have to wait. {grin}

      -- Randal L. Schwartz, Perl hacker

        I was so happy by the appearance of Fork in the Win32 environment that I updated as soon as 5.6 came out.

        My laptop may run Linux, but at work we have to use W2K... :)
RE: Problems with File::Find
by ncw (Friar) on Aug 16, 2000 at 01:55 UTC
    I don't know whether this is the problem in this case but one thing that has caught me out several times in the past is that File::Find uses unix specific features to speed up enumeration of directories under unix-like OSes.

    If you try a basic File::Find like the above on a DOS/VFAT/ISO9660 (CDROM) partition then it will fail in pretty much the way you describe IIRC.

    To fix it, put this in...

    use File::Find; $File::Find::dont_use_nlink=1; # for CDROMS, DOS etc
(jeffa) Re: Problems with File::Find
by jeffa (Bishop) on Aug 15, 2000 at 22:33 UTC
    Try this:
    use strict; use File::Find; @ARGV = ('.') unless @ARGV; find sub { print "$_: ", -s, "\n" }, @ARGV;
    Hope this helps,
    Jeff (trembling at posting after merlyn)