R – Why doesn’t File::Find handle the broken symlink

file-findperlsymlink

I'm using Perl's File::Find module to scan for files, directories, and links. Among other things, I want the utility I'm writing to report broken (dangling in File::Find's parlance) symbolic links. In theory, this is supported by creating a subroutine to be called whenever a broken link has been found, and calling the find method with a hash reference of appropriate values, such as:

my %options = (
   wanted            => \&ProcessFile,
   follow            => 1,
   follow_skip       => 2,
   dangling_symlinks => \&Dangling
);

find(\%options, @ARGV);

Despite deliberately creating a broken link to test this, File::Find never, ever calls the subroutine Dangling. Everything else works except this feature, i.e. the ProcessFile sub gets called as expected, links are followed, etc.

Best Answer

Created test.pl in my home directory:

#!/usr/bin/perl

use File::Find;

my %options = ( wanted => \&ProcessFile,
                follow => 1,
                follow_skip => 2,
                dangling_symlinks => \&Dangling );

find(\%options, @ARGV);

sub ProcessFile {
  print "ProcessFile ($File::Find::name in $File::Find::dir)\n";
}

sub Dangling {
  my ($name, $dir) = @_;
  print "Dangling ($name in $dir)\n";
}

Then:

    $ chmod 755 test.pl

    $ mkdir /tmp/findtest
    $ cd /tmp/findtest
    $ ln -s /tmp/doesnotexist linkylink
    $ ~/test.pl .

Results in:

ProcessFile (. in .)
Dangling (linkylink in ./)
ProcessFile (./linkylink in .)
Related Topic