Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

Module::Build::Cookbook

Perl 5 version 20.0 documentation
Recently read

Module::Build::Cookbook

NAME

Module::Build::Cookbook - Examples of Module::Build Usage

DESCRIPTION

Module::Build isn't conceptually very complicated, but examples are always helpful. The following recipes should help developers and/or installers put together the pieces from the other parts of the documentation.

BASIC RECIPES

Installing modules that use Module::Build

In most cases, you can just issue the following commands:

  1. perl Build.PL
  2. ./Build
  3. ./Build test
  4. ./Build install

There's nothing complicated here - first you're running a script called Build.PL, then you're running a (newly-generated) script called Build and passing it various arguments.

The exact commands may vary a bit depending on how you invoke perl scripts on your system. For instance, if you have multiple versions of perl installed, you can install to one particular perl's library directories like so:

  1. /usr/bin/perl5.8.1 Build.PL
  2. ./Build
  3. ./Build test
  4. ./Build install

If you're on Windows where the current directory is always searched first for scripts, you'll probably do something like this:

  1. perl Build.PL
  2. Build
  3. Build test
  4. Build install

On the old Mac OS (version 9 or lower) using MacPerl, you can double-click on the Build.PL script to create the Build script, then double-click on the Build script to run its build , test , and install actions.

The Build script knows what perl was used to run Build.PL, so you don't need to re-invoke the Build script with the complete perl path each time. If you invoke it with the wrong perl path, you'll get a warning or a fatal error.

Modifying Config.pm values

Module::Build relies heavily on various values from perl's Config.pm to do its work. For example, default installation paths are given by installsitelib and installvendorman3dir and friends, C linker & compiler settings are given by ld , lddlflags , cc , ccflags , and so on. If you're pretty sure you know what you're doing, you can tell Module::Build to pretend there are different values in Config.pm than what's really there, by passing arguments for the --config parameter on the command line:

  1. perl Build.PL --config cc=gcc --config ld=gcc

Inside the Build.PL script the same thing can be accomplished by passing values for the config parameter to new() :

  1. my $build = Module::Build->new
  2. (
  3. ...
  4. config => { cc => 'gcc', ld => 'gcc' },
  5. ...
  6. );

In custom build code, the same thing can be accomplished by calling the config in Module::Build method:

  1. $build->config( cc => 'gcc' ); # Set
  2. $build->config( ld => 'gcc' ); # Set
  3. ...
  4. my $linker = $build->config('ld'); # Get

Installing modules using the programmatic interface

If you need to build, test, and/or install modules from within some other perl code (as opposed to having the user type installation commands at the shell), you can use the programmatic interface. Create a Module::Build object (or an object of a custom Module::Build subclass) and then invoke its dispatch() method to run various actions.

  1. my $build = Module::Build->new
  2. (
  3. module_name => 'Foo::Bar',
  4. license => 'perl',
  5. requires => { 'Some::Module' => '1.23' },
  6. );
  7. $build->dispatch('build');
  8. $build->dispatch('test', verbose => 1);
  9. $build->dispatch('install');

The first argument to dispatch() is the name of the action, and any following arguments are named parameters.

This is the interface we use to test Module::Build itself in the regression tests.

Installing to a temporary directory

To create packages for package managers like RedHat's rpm or Debian's deb , you may need to install to a temporary directory first and then create the package from that temporary installation. To do this, specify the destdir parameter to the install action:

  1. ./Build install --destdir /tmp/my-package-1.003

This essentially just prepends all the installation paths with the /tmp/my-package-1.003 directory.

Installing to a non-standard directory

To install to a non-standard directory (for example, if you don't have permission to install in the system-wide directories), you can use the install_base or prefix parameters:

  1. ./Build install --install_base /foo/bar

See INSTALL PATHS in Module::Build for a much more complete discussion of how installation paths are determined.

Installing in the same location as ExtUtils::MakeMaker

With the introduction of --prefix in Module::Build 0.28 and INSTALL_BASE in ExtUtils::MakeMaker 6.31 its easy to get them both to install to the same locations.

First, ensure you have at least version 0.28 of Module::Build installed and 6.31 of ExtUtils::MakeMaker . Prior versions have differing (and in some cases quite strange) installation behaviors.

The following installation flags are equivalent between ExtUtils::MakeMaker and Module::Build .

  1. MakeMaker Module::Build
  2. PREFIX=... --prefix ...
  3. INSTALL_BASE=... --install_base ...
  4. DESTDIR=... --destdir ...
  5. LIB=... --install_path lib=...
  6. INSTALLDIRS=... --installdirs ...
  7. INSTALLDIRS=perl --installdirs core
  8. UNINST=... --uninst ...
  9. INC=... --extra_compiler_flags ...
  10. POLLUTE=1 --extra_compiler_flags -DPERL_POLLUTE

For example, if you are currently installing MakeMaker modules with this command:

  1. perl Makefile.PL PREFIX=~
  2. make test
  3. make install UNINST=1

You can install into the same location with Module::Build using this:

  1. perl Build.PL --prefix ~
  2. ./Build test
  3. ./Build install --uninst 1

prefix vs install_base

The behavior of prefix is complicated and depends on how your Perl is configured. The resulting installation locations will vary from machine to machine and even different installations of Perl on the same machine. Because of this, it's difficult to document where prefix will place your modules.

In contrast, install_base has predictable, easy to explain installation locations. Now that Module::Build and MakeMaker both have install_base there is little reason to use prefix other than to preserve your existing installation locations. If you are starting a fresh Perl installation we encourage you to use install_base . If you have an existing installation installed via prefix , consider moving it to an installation structure matching install_base and using that instead.

Running a single test file

Module::Build supports running a single test, which enables you to track down errors more quickly. Use the following format:

  1. ./Build test --test_files t/mytest.t

In addition, you may want to run the test in verbose mode to get more informative output:

  1. ./Build test --test_files t/mytest.t --verbose 1

I run this so frequently that I define the following shell alias:

  1. alias t './Build test --verbose 1 --test_files'

So then I can just execute t t/mytest.t to run a single test.

ADVANCED RECIPES

Making a CPAN.pm-compatible distribution

New versions of CPAN.pm understand how to use a Build.PL script, but old versions don't. If authors want to help users who have old versions, some form of Makefile.PL should be supplied. The easiest way to accomplish this is to use the create_makefile_pl parameter to Module::Build->new() in the Build.PL script, which can create various flavors of Makefile.PL during the dist action.

As a best practice, we recommend using the "traditional" style of Makefile.PL unless your distribution has needs that can't be accomplished that way.

The Module::Build::Compat module, which is part of Module::Build 's distribution, is responsible for creating these Makefile.PLs. Please see Module::Build::Compat for the details.

Changing the order of the build process

The build_elements property specifies the steps Module::Build will take when building a distribution. To change the build order, change the order of the entries in that property:

  1. # Process pod files first
  2. my @e = @{$build->build_elements};
  3. my ($i) = grep {$e[$_] eq 'pod'} 0..$#e;
  4. unshift @e, splice @e, $i, 1;

Currently, build_elements has the following default value:

  1. [qw( PL support pm xs pod script )]

Do take care when altering this property, since there may be non-obvious (and non-documented!) ordering dependencies in the Module::Build code.

Adding new file types to the build process

Sometimes you might have extra types of files that you want to install alongside the standard types like .pm and .pod files. For instance, you might have a Bar.dat file containing some data related to the Foo::Bar module and you'd like for it to end up as Foo/Bar.dat somewhere in perl's @INC path so Foo::Bar can access it easily at runtime. The following code from a sample Build.PL file demonstrates how to accomplish this:

  1. use Module::Build;
  2. my $build = Module::Build->new
  3. (
  4. module_name => 'Foo::Bar',
  5. ...other stuff here...
  6. );
  7. $build->add_build_element('dat');
  8. $build->create_build_script;

This will find all .dat files in the lib/ directory, copy them to the blib/lib/ directory during the build action, and install them during the install action.

If your extra files aren't located in the lib/ directory in your distribution, you can explicitly say where they are, just as you'd do with .pm or .pod files:

  1. use Module::Build;
  2. my $build = new Module::Build
  3. (
  4. module_name => 'Foo::Bar',
  5. dat_files => {'some/dir/Bar.dat' => 'lib/Foo/Bar.dat'},
  6. ...other stuff here...
  7. );
  8. $build->add_build_element('dat');
  9. $build->create_build_script;

If your extra files actually need to be created on the user's machine, or if they need some other kind of special processing, you'll probably want to subclass Module::Build and create a special method to process them, named process_${kind}_files():

  1. use Module::Build;
  2. my $class = Module::Build->subclass(code => <<'EOF');
  3. sub process_dat_files {
  4. my $self = shift;
  5. ... locate and process *.dat files,
  6. ... and create something in blib/lib/
  7. }
  8. EOF
  9. my $build = $class->new
  10. (
  11. module_name => 'Foo::Bar',
  12. ...other stuff here...
  13. );
  14. $build->add_build_element('dat');
  15. $build->create_build_script;

If your extra files don't go in lib/ but in some other place, see Adding new elements to the install process for how to actually get them installed.

Please note that these examples use some capabilities of Module::Build that first appeared in version 0.26. Before that it could still be done, but the simple cases took a bit more work.

Adding new elements to the install process

By default, Module::Build creates seven subdirectories of the blib directory during the build process: lib, arch, bin, script, bindoc, libdoc, and html (some of these may be missing or empty if there's nothing to go in them). Anything copied to these directories during the build will eventually be installed during the install action (see INSTALL PATHS in Module::Build.

If you need to create a new custom type of installable element, e.g. conf , then you need to tell Module::Build where things in blib/conf/ should be installed. To do this, use the install_path parameter to the new() method:

  1. my $build = Module::Build->new
  2. (
  3. ...other stuff here...
  4. install_path => { conf => $installation_path }
  5. );

Or you can call the install_path() method later:

  1. $build->install_path(conf => $installation_path);

The user may also specify the path on the command line:

  1. perl Build.PL --install_path conf=/foo/path/etc

The important part, though, is that somehow the install path needs to be set, or else nothing in the blib/conf/ directory will get installed, and a runtime error during the install action will result.

See also Adding new file types to the build process for how to create the stuff in blib/conf/ in the first place.

EXAMPLES ON CPAN

Several distributions on CPAN are making good use of various features of Module::Build. They can serve as real-world examples for others.

SVN-Notify-Mirror

http://search.cpan.org/~jpeacock/SVN-Notify-Mirror/

John Peacock, author of the SVN-Notify-Mirror distribution, says:

1.
Using auto_features , I check to see whether two optional modules are available - SVN::Notify::Config and Net::SSH;
2.
If the S::N::Config module is loaded, I automatically generate test files for it during Build (using the PL_files property).
3.
If the ssh_feature is available, I ask if the user wishes to perform the ssh tests (since it requires a little preliminary setup);
4.
Only if the user has ssh_feature and answers yes to the testing, do I generate a test file.

I'm sure I could not have handled this complexity with EU::MM, but it was very easy to do with M::B.

Modifying an action

Sometimes you might need an to have an action, say ./Build install, do something unusual. For instance, you might need to change the ownership of a file or do something else peculiar to your application.

You can subclass Module::Build on the fly using the subclass() method and override the methods that perform the actions. You may need to read through Module::Build::Authoring and Module::Build::API to find the methods you want to override. All "action" methods are implemented by a method called "ACTION_" followed by the action's name, so here's an example of how it would work for the install action:

  1. # Build.PL
  2. use Module::Build;
  3. my $class = Module::Build->subclass(
  4. class => "Module::Build::Custom",
  5. code => <<'SUBCLASS' );
  6. sub ACTION_install {
  7. my $self = shift;
  8. # YOUR CODE HERE
  9. $self->SUPER::ACTION_install;
  10. }
  11. SUBCLASS
  12. $class->new(
  13. module_name => 'Your::Module',
  14. # rest of the usual Module::Build parameters
  15. )->create_build_script;

Adding an action

You can add a new ./Build action simply by writing the method for it in your subclass. Use depends_on to declare that another action must have been run before your action.

For example, let's say you wanted to be able to write ./Build commit to test your code and commit it to Subversion.

  1. # Build.PL
  2. use Module::Build;
  3. my $class = Module::Build->subclass(
  4. class => "Module::Build::Custom",
  5. code => <<'SUBCLASS' );
  6. sub ACTION_commit {
  7. my $self = shift;
  8. $self->depends_on("test");
  9. $self->do_system(qw(svn commit));
  10. }
  11. SUBCLASS

Bundling Module::Build

Note: This section probably needs an update as the technology improves (see contrib/bundle.pl in the distribution).

Suppose you want to use some new-ish features of Module::Build, e.g. newer than the version of Module::Build your users are likely to already have installed on their systems. The first thing you should do is set configure_requires to your minimum version of Module::Build. See Module::Build::Authoring.

But not every build system honors configure_requires yet. Here's how you can ship a copy of Module::Build, but still use a newer installed version to take advantage of any bug fixes and upgrades.

First, install Module::Build into Your-Project/inc/Module-Build. CPAN will not index anything in the inc directory so this copy will not show up in CPAN searches.

  1. cd Module-Build
  2. perl Build.PL --install_base /path/to/Your-Project/inc/Module-Build
  3. ./Build test
  4. ./Build install

You should now have all the Module::Build .pm files in Your-Project/inc/Module-Build/lib/perl5.

Next, add this to the top of your Build.PL.

  1. my $Bundled_MB = 0.30; # or whatever version it was.
  2. # Find out what version of Module::Build is installed or fail quietly.
  3. # This should be cross-platform.
  4. my $Installed_MB =
  5. `$^X -e "eval q{require Module::Build; print Module::Build->VERSION} or exit 1";
  6. # some operating systems put a newline at the end of every print.
  7. chomp $Installed_MB;
  8. $Installed_MB = 0 if $?;
  9. # Use our bundled copy of Module::Build if it's newer than the installed.
  10. unshift @INC, "inc/Module-Build/lib/perl5" if $Bundled_MB > $Installed_MB;
  11. require Module::Build;

And write the rest of your Build.PL normally. Module::Build will remember your change to @INC and use it when you run ./Build.

In the future, we hope to provide a more automated solution for this scenario; see inc/latest.pm in the Module::Build distribution for one indication of the direction we're moving.

AUTHOR

Ken Williams <kwilliams@cpan.org>

COPYRIGHT

Copyright (c) 2001-2008 Ken Williams. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

perl(1), Module::Build(3), Module::Build::Authoring(3), Module::Build::API(3)