Saturday, February 6

Getting a list of files in a given target directory matching some criteria, in Perl, using File::Find:

use File:Find;

# collect a list of things to render
sub get_source_files {
  my ($root_dir) = @_;

  my @source_files;
  find(
    sub {
      if (! /index$/) {
        push @source_files, $File::Find::name;
      }
    },
    $root_dir
  );
  return @source_files;
}

Notes: You can use $_ inside the subroutine, but if you want the full path, you need to use $File::Find::name. For some reason you can’t do the normal thing where you pull arguments off of @_.

Experientially, File::Find is actually kind of a crap replacement for shelling out to find(1). I seem to have used it a bunch of times over the years, but I never remember how the interface works and every time I find out I’m irritated.