Question

I am converting an inherited software collection to a Module::Build based distribution.

The lib directories contain, in addition to the .pm files, certain external files needed by the modules. It is easy to ensure that the ones that have extensions are copied along with the .pm files by using Module::Build's add_build_element method.

But I am confused about how to deal with files that have no extensions. How can I make sure those files are also copied alongside the .pm files during installation? Is there a way to tell Module::Build to copy everything under lib?

Was it helpful?

Solution

Build.PL

use lib 'inc';
use Local::Module::Build::Extensionless;

my $build = Local::Module::Build::Extensionless->new(
    module_name => 'Foo::Bar',
    license     => 'restricted',
);
$build->add_build_element('lib');
$build->create_build_script;

inc/Local/Module/Build/Extensionless.pm

package Local::Module::Build::Extensionless;
use parent 'Module::Build';
use File::Next qw();

sub process_lib_files {
    my ($self) = @_;
    my $files;

    {
        my $iter = File::Next::files('lib');
        while (defined(my $file = $iter->())) {
            $files->{$file} = $file;
        }
    }

    # following piece from Module::Build::Base::process_files_by_extension
    while (my ($file, $dest) = each %$files) {
        $self->copy_if_modified(from => $file, to => File::Spec->catfile($self->blib, $dest));
    }
};
1;

But why so complicated? You really want share_dir.

OTHER TIPS

If these are module specific data files; there's a somewhat obscure convention that data owned by lib/Acme/Foo/Bar.pm is usually placed in lib/auto/Acme/Foo/Bar/.

Most packers including Module::Build should respect this convention and automatically treat this as a "data payload" and package it along with the module files.

There's some helper modules including File::ShareDir that can help you locate the data at runtime.

use File::ShareDir;
my $data_dir = File::ShareDir::module_dir('Acme::Foo::Bar');

Just one possible approach. I'm hoping it might fit your detailed requirements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top