perlfile-find

Find:File in Perl - Search through symlink directory


Does anyone know how to make File:Find can search through symlink directory?

I have a real directory at

/home/alex/mydir1 

and a symlink directory inside

/home/alex/mydir1/test -> ../mydir2

Here is my code:

#!/depot/perl-5.8.3/bin/perl
use strict;
use File::Find qw(find);

my $path = "/home/alex/mydir1";

find(\&Search,follow => 1, $path);

sub Search{
    my $path = $File::Find::name;
    print $path."\n";
}

And the result is:

/home/alex/mydir1
/home/alex/mydir1/test

Why it's not search through /home/alex/mydir2 and print out every files inside ? Can anyone show me how to do that ?

Thank you and best regards.

Alex


Solution

  • A close look at the documentation for File::Find reveals the error: You have passed a key-value list of parameters instead of a reference to a hash of parameters.

    # Incorrect: looks like find(@params)
    # asks find to search the list of paths:
    #    ( 'follow', 1, $path )
    find(\&Search,follow => 1, $path);
    
    # Correct: looks like find(\%params)
    find({ wanted => \&process, follow => 1 }, $path);