<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Module::Build::Cookbook;
use strict;
use warnings;
our $VERSION = '0.4210';


=head1 NAME

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

=head1 DESCRIPTION

C&lt;Module::Build&gt; 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.


=head1 BASIC RECIPES


=head2 Installing modules that use Module::Build

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

  perl Build.PL
  ./Build
  ./Build test
  ./Build install

There's nothing complicated here - first you're running a script
called F&lt;Build.PL&gt;, then you're running a (newly-generated) script
called F&lt;Build&gt; 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:

  /usr/bin/perl5.8.1 Build.PL
  ./Build
  ./Build test
  ./Build install

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

  perl Build.PL
  Build
  Build test
  Build install

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

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

=head2 Modifying Config.pm values

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

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

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

 my $build = Module::Build-&gt;new
   (
    ...
    config =&gt; { cc =&gt; 'gcc', ld =&gt; 'gcc' },
    ...
   );

In custom build code, the same thing can be accomplished by calling
the L&lt;Module::Build/config&gt; method:

 $build-&gt;config( cc =&gt; 'gcc' );     # Set
 $build-&gt;config( ld =&gt; 'gcc' );     # Set
 ...
 my $linker = $build-&gt;config('ld'); # Get


=head2 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 C&lt;dispatch()&gt; method to run various
actions.

  my $build = Module::Build-&gt;new
    (
     module_name =&gt; 'Foo::Bar',
     license     =&gt; 'perl',
     requires    =&gt; { 'Some::Module'   =&gt; '1.23' },
    );
  $build-&gt;dispatch('build');
  $build-&gt;dispatch('test', verbose =&gt; 1);
  $build-&gt;dispatch('install');

The first argument to C&lt;dispatch()&gt; 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.


=head2 Installing to a temporary directory

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

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

This essentially just prepends all the installation paths with the
F&lt;/tmp/my-package-1.003&gt; directory.


=head2 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
C&lt;install_base&gt; or C&lt;prefix&gt; parameters:

  ./Build install --install_base /foo/bar

See L&lt;Module::Build/"INSTALL PATHS"&gt; for a much more complete
discussion of how installation paths are determined.


=head2 Installing in the same location as ExtUtils::MakeMaker

With the introduction of C&lt;--prefix&gt; in Module::Build 0.28 and
C&lt;INSTALL_BASE&gt; in C&lt;ExtUtils::MakeMaker&gt; 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 C&lt;ExtUtils::MakeMaker&gt;.  Prior versions have
differing (and in some cases quite strange) installation behaviors.

The following installation flags are equivalent between
C&lt;ExtUtils::MakeMaker&gt; and C&lt;Module::Build&gt;.

    MakeMaker             Module::Build
    PREFIX=...            --prefix ...
    INSTALL_BASE=...      --install_base ...
    DESTDIR=...           --destdir ...
    LIB=...               --install_path lib=...
    INSTALLDIRS=...       --installdirs ...
    INSTALLDIRS=perl      --installdirs core
    UNINST=...            --uninst ...
    INC=...               --extra_compiler_flags ...
    POLLUTE=1             --extra_compiler_flags -DPERL_POLLUTE

For example, if you are currently installing C&lt;MakeMaker&gt; modules with
this command:

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

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

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

=head3 C&lt;prefix&gt; vs C&lt;install_base&gt;

The behavior of C&lt;prefix&gt; 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 C&lt;prefix&gt; will place your modules.

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


=head2 Running a single test file

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

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

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

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

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

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

So then I can just execute C&lt;t t/mytest.t&gt; to run a single test.


=head1 ADVANCED RECIPES


=head2 Making a CPAN.pm-compatible distribution

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

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

The C&lt;Module::Build::Compat&gt; module, which is part of
C&lt;Module::Build&gt;'s distribution, is responsible for creating these
F&lt;Makefile.PL&gt;s.  Please see L&lt;Module::Build::Compat&gt; for the details.


=head2 Changing the order of the build process

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

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

Currently, C&lt;build_elements&gt; has the following default value:

  [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
C&lt;Module::Build&gt; code.


=head2 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 F&lt;.pm&gt; and F&lt;.pod&gt; files.  For
instance, you might have a F&lt;Bar.dat&gt; file containing some data
related to the C&lt;Foo::Bar&gt; module and you'd like for it to end up as
F&lt;Foo/Bar.dat&gt; somewhere in perl's C&lt;@INC&gt; path so C&lt;Foo::Bar&gt; can
access it easily at runtime.  The following code from a sample
C&lt;Build.PL&gt; file demonstrates how to accomplish this:

  use Module::Build;
  my $build = Module::Build-&gt;new
    (
     module_name =&gt; 'Foo::Bar',
     ...other stuff here...
    );
  $build-&gt;add_build_element('dat');
  $build-&gt;create_build_script;

This will find all F&lt;.dat&gt; files in the F&lt;lib/&gt; directory, copy them
to the F&lt;blib/lib/&gt; directory during the C&lt;build&gt; action, and install
them during the C&lt;install&gt; action.

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

  use Module::Build;
  my $build = new Module::Build
    (
     module_name =&gt; 'Foo::Bar',
     dat_files =&gt; {'some/dir/Bar.dat' =&gt; 'lib/Foo/Bar.dat'},
     ...other stuff here...
    );
  $build-&gt;add_build_element('dat');
  $build-&gt;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 C&lt;Module::Build&gt; and create a special method to
process them, named C&lt;process_${kind}_files()&gt;:

  use Module::Build;
  my $class = Module::Build-&gt;subclass(code =&gt; &lt;&lt;'EOF');
    sub process_dat_files {
      my $self = shift;
      ... locate and process *.dat files,
      ... and create something in blib/lib/
    }
  EOF
  my $build = $class-&gt;new
    (
     module_name =&gt; 'Foo::Bar',
     ...other stuff here...
    );
  $build-&gt;add_build_element('dat');
  $build-&gt;create_build_script;

If your extra files don't go in F&lt;lib/&gt; but in some other place, see
L&lt;"Adding new elements to the install process"&gt; 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.


=head2 Adding new elements to the install process

By default, Module::Build creates seven subdirectories of the F&lt;blib&gt;
directory during the build process: F&lt;lib&gt;, F&lt;arch&gt;, F&lt;bin&gt;,
F&lt;script&gt;, F&lt;bindoc&gt;, F&lt;libdoc&gt;, and F&lt;html&gt; (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 C&lt;install&gt; action (see L&lt;Module::Build/"INSTALL PATHS"&gt;.

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

  my $build = Module::Build-&gt;new
    (
     ...other stuff here...
     install_path =&gt; { conf =&gt; $installation_path }
    );

Or you can call the C&lt;install_path()&gt; method later:

  $build-&gt;install_path(conf =&gt; $installation_path);

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

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

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

See also L&lt;"Adding new file types to the build process"&gt; for how to
create the stuff in F&lt;blib/conf/&gt; in the first place.


=head1 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.


=head2 SVN-Notify-Mirror

L&lt;http://search.cpan.org/~jpeacock/SVN-Notify-Mirror/&gt;

John Peacock, author of the C&lt;SVN-Notify-Mirror&gt; distribution, says:

=over 4

=item 1. Using C&lt;auto_features&gt;, I check to see whether two optional
modules are available - SVN::Notify::Config and Net::SSH;

=item 2. If the S::N::Config module is loaded, I automatically
generate test files for it during Build (using the C&lt;PL_files&gt;
property).

=item 3. If the C&lt;ssh_feature&gt; is available, I ask if the user wishes
to perform the ssh tests (since it requires a little preliminary
setup);

=item 4. Only if the user has C&lt;ssh_feature&gt; 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.

=back


=head2 Modifying an action

Sometimes you might need an to have an action, say C&lt;./Build install&gt;,
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 C&lt;Module::Build&gt; on the fly using the C&lt;subclass()&gt;
method and override the methods that perform the actions.  You may
need to read through C&lt;Module::Build::Authoring&gt; and
C&lt;Module::Build::API&gt; 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 C&lt;install&gt; action:

  # Build.PL
  use Module::Build;
  my $class = Module::Build-&gt;subclass(
      class =&gt; "Module::Build::Custom",
      code =&gt; &lt;&lt;'SUBCLASS' );

  sub ACTION_install {
      my $self = shift;
      # YOUR CODE HERE
      $self-&gt;SUPER::ACTION_install;
  }
  SUBCLASS

  $class-&gt;new(
      module_name =&gt; 'Your::Module',
      # rest of the usual Module::Build parameters
  )-&gt;create_build_script;


=head2 Adding an action

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

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

  # Build.PL
  use Module::Build;
  my $class = Module::Build-&gt;subclass(
      class =&gt; "Module::Build::Custom",
      code =&gt; &lt;&lt;'SUBCLASS' );

  sub ACTION_commit {
      my $self = shift;

      $self-&gt;depends_on("test");
      $self-&gt;do_system(qw(svn commit));
  }
  SUBCLASS


=head2 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 C&lt;configure_requires&gt; to your minimum version of
Module::Build.  See L&lt;Module::Build::Authoring&gt;.

But not every build system honors C&lt;configure_requires&gt; 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 F&lt;Your-Project/inc/Module-Build&gt;.
CPAN will not index anything in the F&lt;inc&gt; directory so this copy will
not show up in CPAN searches.

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

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

Next, add this to the top of your F&lt;Build.PL&gt;.

    my $Bundled_MB = 0.30;  # or whatever version it was.

    # Find out what version of Module::Build is installed or fail quietly.
    # This should be cross-platform.
    my $Installed_MB =
        `$^X -e "eval q{require Module::Build; print Module::Build-&gt;VERSION} or exit 1";

    # some operating systems put a newline at the end of every print.
    chomp $Installed_MB;

    $Installed_MB = 0 if $?;

    # Use our bundled copy of Module::Build if it's newer than the installed.
    unshift @INC, "inc/Module-Build/lib/perl5" if $Bundled_MB &gt; $Installed_MB;

    require Module::Build;

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

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


=head1 AUTHOR

Ken Williams &lt;kwilliams@cpan.org&gt;


=head1 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.


=head1 SEE ALSO

perl(1), L&lt;Module::Build&gt;(3), L&lt;Module::Build::Authoring&gt;(3),
L&lt;Module::Build::API&gt;(3)

=cut
</pre></body></html>