yahan
yahan

Reputation: 81

Can a Perl module distribution include a script

I'm considering of using modules to group scripts. I often end up in the scenario that I have one or more scripts that uses a certain module.

Is it possible to pack the scripts together with the module?

Are there any specifics to how to achieve that?

Thanks for your input.

Upvotes: 1

Views: 179

Answers (1)

stevieb
stevieb

Reputation: 9296

You can include scripts along with your module by including the EXE_FILES directive in a Makefile.PL file, and supplying the relative path to the scripts in an array reference. In the below example, the scripts live in a bin/ directory within the top level of your module's directory. You do not need to have your files in a distribution-like layout, but it helps (see below).

After setting up your directory structure and fiddling with the paths within the Makefile.PL, running make install will install your scripts along with your module.

use ExtUtils::MakeMaker;

WriteMakefile(
    NAME              => 'My::Module',
    VERSION_FROM      => 'lib/My/Module.pm',
    ($] >= 5.005 ?
      (ABSTRACT_FROM  => 'lib/My/Module.pm',
    AUTHOR            => 'My Name <email.addr>') : ()),
    LIBS              => [],
    EXE_FILES         => [qw(bin/script1 bin/script2)],
    DEFINE            => '',
    INC               => '-I.',
    PREREQ_PM         => {},
);

Personally, I'd recommend turning your module into a full-blown distribution. Here's an example. First, install Module::Starter, then:

module-starter --author="My Name" --email="[email protected]" --module=My::Module --eumm

Now you can copy your module's code to lib/My/Module.pm, create a bin/ directory, put your scripts in it, add the EXE_FILES directive to the Makefile.PL, and you can use the full suite of make commands to make, test and install your modules and the scripts, eg:

perl Makefile.PL
make
make test
make install

Here's a real-world example, one of my own distributions on the CPAN that includes a bundled pinmap Perl script along with the distribution's modules:

Upvotes: 3

Related Questions