Credit: Image generated by Gemini

Every developer has had this experience. You write some code that is almost idiomatic. Then you write it again. Then you find yourself writing it yet again…maybe I should codify this? Nah, it’s too easy to just cobble together those three classes, add a few checks and be done with it. Why introduce another dependency?

Ask the question differently next time you find yourself writing boilerplate code that is just a bit different each time but essentially accomplishes the same exact thing.

Why should I use 3 classes and a couple dozen lines of code, when 1 class and two lines would suffice?

You hesitate not because the problem is difficult. Quite the opposite. Because the solution is so embarrassingly simple your muscle memory keeps tapping out the same ‘ol tired recipe.

use File::Copy qw(cp);
use File::ShareDir qw(dist_file);

cp dist_file('Bedrock', 'tagx.xml'), "$dest_dir/tagx.xml";

…wait let me generalize that ‘cuz I need to copy a few more files…

# version 2!
use File::Copy qw(cp);
use File::ShareDir qw(dist_file);

sub cp_dist_file {
  my ($dest_dir, $dist, $file) = @_;

  my $dist_file = dist_file($dist, $file);

  cp $dist_file, $dest_dir;

  return "$dest_dir/$file";
}

…oh wait, what if $file is a path like config/tagx.xml?

# version 3!
use File::Copy qw(cp);
use File::ShareDir qw(dist_dir);
use File::Basename qw(basename);
use File::Spec;

sub cp_dist_file {
  my ($dest_dir, $dist, $file) = @_;

  my $dist_dir = dist_dir($dist);

  my $dist_file = File::Spec->catfile($dist_dir, $file);
  my $path = File::Spec->catfile($dest_dir, basename($file) );

  cp $dist_file, $path;

  return $path;
}

…oh boy…no error checking? What if the destination directory doesn’t exist? Then we might need File::Path.

And perhaps we should check whether the source exists, whether the destination is a directory or filename, and whether we’re copying an entire directory tree.

Before long, what started as a simple file copy has become another little collection of filesystem housekeeping code. Or it doesn’t, and you just accept version 1 and sprinkle it around your application.

Maybe CPAN has a module that would satisfy my itch for brevity and succinctness?

Surely CPAN Has Something?

A quick survey of MetaCPAN reveals a number of modules that almost fit the bill.

Module What it provides What’s missing for this use case
File::ShareDir Locates installed distribution and module resources. No copying, recursive copying, or CLI.
File::Share Resource lookup with support for local development share directories. Still primarily about finding resources, not copying them.
File::ShareDir::Dist Distribution-specific access to shared configuration and data. Not a general-purpose resource copying interface.
File::Copy Copies individual files with copy() or cp(). Doesn’t know about installed distribution resources or recursively copy directory trees.

So none of these is quite right. What might be useful is something like my mythical File::DistDir:

# Where are the distribution's shared files?
dist-dir -d Bedrock dir

# List the distribution contents
dist-dir -d Bedrock ls

# Copy a file from the distribution directory
dist-dir -d Bedrock cp config/tagx.xml ./config/

And because copying an entire directory seemed like something I’d eventually want to do:

dist-dir -d Bedrock -r cp config ./config

Or perhaps grab the whole kit and kaboodle:

dist-dir -d Bedrock -r cp .

But then it occurred to me that I might need this same functionality in a Perl application.

So naturally, I added an object-oriented interface.

use File::DistDir;

my $dist = File::DistDir->new(
  distribution => 'Bedrock'
);

$dist->copy('config/tagx.xml', './config/tagx.xml');

$dist->copy('config', './config', recursive => 1);

There. Done. No more less-than-ideal boilerplate.

But Does CPAN Really Need Another Module?

Probably not. But after all…

Why should I use 3 classes and a couple dozen lines of code, when 1 class and two lines would suffice?

You can find File::DistDir here.


Previous post: Adventures in CPAN Distribution Installation: Hare We Go Again, Down the Rabbit Hole