App-GetClosestFile

 view release on metacpan or  search on metacpan

lib/App/GetClosestFile.pm  view on Meta::CPAN

}

if (!$filename) {
  usage();
}

sub usage {
  print "\ngetclosest [options] filename

    options:

      --all       show all matches
      --break     print results on separate lines
      --depth N   recursion depth
      --help      show this message
      
Version $VERSION.\n\n";

  exit;
}

sub count_depth {
  my $output = 0;

  if ($root) {
    my $path = shift;
    $path = $path =~ s/$root\///r;
    my @parts = split '', $path;

    for (@parts) {
      if ($_ eq '/') {
        $output++;
      }
    }
  }

  return $output;
}

sub read_dir {
  my $dir;
  my $dirname = shift;

  $current_depth = count_depth $dirname;

  if ($recursion_depth > 0) {
    if ($current_depth > $recursion_depth) {
      return;
    }
  }

  opendir($dir, $dirname) or die;

  my @files = readdir($dir);

  foreach my $file (@files) {
    if ($file eq "." or $file eq "..") {
      next;
    }

    stat($dirname . "/" . $file);

    if (-d _) {
      push @child_dirs, $dirname . "/" . $file;
    } else {
      if ($file =~ /$filename/) {
        print $dirname . "/" . $file;

        if ($show_all_matches == 1) {
          if ($line_breaks) {
            print "\n";
          } else {
            print " ";
          }
        } else {
          exit;
        }
      }
    }
  }
}

sub run {
  $root = getcwd;
  read_dir($root, 1);

  while (my $size = @child_dirs > 0) {
    my $dir = shift @child_dirs;
    read_dir($dir);
  }
}

1;



( run in 0.604 second using v1.01-cache-2.11-cpan-39bf76dae61 )