Install scripts from distributions

Perl’s distribution system is quite powerful and supported by a variety of tools that can make life easier for you. Most people tend to think that “distributions” are synonymous with modules, but that’s only one of the uses for distributions.

In Item 91: Write Scripts as Modules, we talk about the creating programs as “modulinos”, or little modules. You segregate everything into subroutines and activate your code through a run() subroutine, similar to the main subroutine you’d use in other languages. I’ve written about this a lot elsewhere, including my TPJ article Scripts As Modules and an entire chapter in Mastering Perl.

A modulino is a great way to start a new program, but it’s often too much work to convert an existing program if you only have a little time available. You can still get a lot of the benefits of a Perl distribution, such as dependency management and installation, even without the module portion.

Typically, in your distribution you put your modules under the lib directory. You’d put your Cat::Burglar module in lib/Cat/ Your build system (Item 79: Use Module::Build) sees what is in lib and copies it into blib/lib, the build library which is the intermediate staging area for the installation. When you install your distribution, the build system copies the contents of blib into the right places. You don’t have to have just modules in your distribution, though.

You can also have a bin directory under blib. The build system copies the contents of the blib/bin. You don’t have to stage your programs manually. Suppose that you stored your program in the distribution as script/cat_burglar, although the location isn’t magical. For Makemaker, you use the EXE_FILES argument to stage the program for installation:

	use ExtUtils::Makemaker;
		'EXE_FILES'    => [ qw(script/cat_burglar) ],

For Module::Build, you use the script_files argument:

my $build = Module::Build->new
	script_files => [ qw(script/cat_burglar) ],

You set up the rest of the build file normally, specifying such things as the prerequisites for configuring, building, and running the program, creating the distribution archive, and so on. You don’t even need to have any modules. You can have just modules, just scripts, or scripts and modules combined. You just have to set up the build file appropriately. Once you’ve done that, you can treat your programs just as you do any other distribution. When you want to install it, you go through the same process that you do with modules:

% perl Build.PL
% ./Build install

You can even use the cpan tool to install from the current directory, including all of its dependencies:

% cpan .

Although I now tend to write most programs of any consequence as modulinos, when I first started putting my programs into distributions I wrote a tool that I called scriptdist to turn single program files into complete distributions. I wrote about that for TPJ as Automating script distributions with scriptdist. Although it’s a bit dated, it’s essentially in the same state. If you don’t like its templates, you can modify them to make your own.

Simply wrapping your program in a distribution is merely a first step, but it’s better than nothing until you have time to pay more attention to it.