# Non-unix folks, replace ~ with /path/to/your/home/dir
perl Makefile.PL INSTALL_BASE=~
This will put modules into ~/lib/perl5, man pages into ~/man and programs into ~/bin.
To ensure your Perl programs can see these newly installed modules, set your "PERL5LIB" environment variable to ~/lib/perl5 or tell each of your programs to look in that directory with the following:
use lib "$ENV{HOME}/lib/perl5";
or if $ENV{HOME} isn't set and you don't want to set it for some reason, do it the long way.
use lib "/path/to/your/home/dir/lib/perl5";
1) Use INSTALL_BASE / "--install_base"
MakeMaker (as of 6.31) and Module::Build (as of 0.28) both can install to the same locations using the ``install_base'' concept. See ``INSTALL_BASE'' in ExtUtils::MakeMaker for details. To get MM and MB to install to the same location simply set INSTALL_BASE in MM and "--install_base" in MB to the same location.
perl Makefile.PL INSTALL_BASE=/whatever
perl Build.PL --install_base /whatever
2) Use PREFIX / "--prefix"
Module::Build 0.28 added support for "--prefix" which works like MakeMaker's PREFIX.
perl Makefile.PL PREFIX=/whatever
perl Build.PL --prefix /whatever
For an individual module:
perl Makefile.PL INSTALLMAN1DIR=none INSTALLMAN3DIR=none
If you want to suppress man page installation for all modules you have to reconfigure Perl and tell it 'none' when it asks where to install man pages.
perl Makefile.PL
make
make test
...and then set the PERL5LIB environment variable to point at the blib/lib and blib/arch directories.
The other is to install the module in a temporary location.
perl Makefile.PL INSTALL_BASE=~/tmp
make
make test
make install
And then set PERL5LIB to ~/tmp/lib/perl5. This works well when you have multiple modules to work with. It also ensures that the module goes through its full installation process which may modify it.
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.
There are many reasons, but the major one is cross-platform compatibility.
Perl is one of the most ported pieces of software ever. It works on operating systems I've never even heard of (see perlport for details). It needs a build tool that can work on all those platforms and with any wacky C compilers and linkers they might have.
No such build tool exists. Even make itself has wildly different dialects. So we have to build our own.
Module::Build is the official heir apparent to MakeMaker and we encourage people to work on M::B rather than spending time adding features to MakeMaker.
Simplest way to do it automatically is to use your version control system's revision number (you are using version control, right?).
In CVS, RCS and SVN you use $Revision$ (see the documentation of your version control system for details). Every time the file is checked in the $Revision$ will be updated, updating your $VERSION.
SVN uses a simple integer for $Revision$ so you can adapt it for your $VERSION like so:
($VERSION) = q$Revision$ =~ /(\d+)/;
In CVS and RCS version 1.9 is followed by 1.10. Since CPAN compares version numbers numerically we use a sprintf() to convert 1.9 to 1.009 and 1.10 to 1.010 which compare properly.
$VERSION = sprintf "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/g;
If branches are involved (ie. $Revision: 1.5.3.4$) its a little more complicated.
# must be all on one line or MakeMaker will get confused.
$VERSION = do { my @r = (q$Revision$ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r };
In SVN, $Revision$ should be the same for every file in the project so they would all have the same $VERSION. CVS and RCS have a different $Revision$ per file so each file will have a differnt $VERSION. Distributed version control systems, such as SVK, may have a different $Revision$ based on who checks out the file leading to a different $VERSION on each machine! Finally, some distributed version control systems, such as darcs, have no concept of revision number at all.
To shut off its generation, pass the "NO_META" flag to "WriteMakefile()".
If you really want to do this, you can use "ExtUtils::Manifest::manifind()" to read the MANIFEST and File::Find to delete the files. But you have to be careful. Here's a script to do that. Use at your own risk. Have fun blowing holes in your foot.
#!/usr/bin/perl -w
use strict;
use File::Spec;
use File::Find;
use ExtUtils::Manifest qw(maniread);
my %manifest = map {( $_ => 1 )}
grep { File::Spec->canonpath($_) }
keys %{ maniread() };
if( !keys %manifest ) {
print "No files found in MANIFEST. Stopping.\n";
exit;
}
find({
wanted => sub {
my $path = File::Spec->canonpath($_);
return unless -f $path;
return if exists $manifest{ $path };
print "unlink $path\n";
unlink $path;
},
no_chdir => 1
},
"."
);
To avoid this, you can force the Makefile to be rebuilt whenever you change the module containing the version number by adding this to your WriteMakefile() arguments.
depend => { '$(FIRST_MAKEFILE)' => '$(VERSION_FROM)' }
Let's assume that we have a package "Cool::Foo", which includes "Cool::Foo" and "Cool::Bar" modules each having a separate XS file. First we use the following Makefile.PL:
use ExtUtils::MakeMaker;
WriteMakefile(
NAME => 'Cool::Foo',
VERSION_FROM => 'Foo.pm',
OBJECT => q/$(O_FILES)/,
# ... other attrs ...
);
Notice the "OBJECT" attribute. MakeMaker generates the following variables in Makefile:
# Handy lists of source code files:
XS_FILES= Bar.xs \
Foo.xs
C_FILES = Bar.c \
Foo.c
O_FILES = Bar.o \
Foo.o
Therefore we can use the "O_FILES" variable to tell MakeMaker to use these objects into the shared library.
That's pretty much it. Now write Foo.pm and Foo.xs, Bar.pm and Bar.xs, where Foo.pm bootstraps the shared library and Bar.pm simply loading Foo.pm.
The only issue left is to how to bootstrap Bar.xs. This is done from Foo.xs:
MODULE = Cool::Foo PACKAGE = Cool::Foo BOOT: # boot the second XS file boot_Cool__Bar(aTHX_ cv);
If you have more than two files, this is the place where you should boot extra XS files from.
The following four files sum up all the details discussed so far.
Foo.pm:
-------
package Cool::Foo;
require DynaLoader;
our @ISA = qw(DynaLoader);
our $VERSION = '0.01';
bootstrap Cool::Foo $VERSION;
1;
Bar.pm:
-------
package Cool::Bar;
use Cool::Foo; # bootstraps Bar.xs
1;
Foo.xs:
-------
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
MODULE = Cool::Foo PACKAGE = Cool::Foo
BOOT:
# boot the second XS file
boot_Cool__Bar(aTHX_ cv);
MODULE = Cool::Foo PACKAGE = Cool::Foo PREFIX = cool_foo_
void
cool_foo_perl_rules()
CODE:
fprintf(stderr, "Cool::Foo says: Perl Rules\n");
Bar.xs:
-------
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
MODULE = Cool::Bar PACKAGE = Cool::Bar PREFIX = cool_bar_
void
cool_bar_perl_rules()
CODE:
fprintf(stderr, "Cool::Bar says: Perl Rules\n");
And of course a very basic test:
t/cool.t:
--------
use Test;
BEGIN { plan tests => 1 };
use Cool::Foo;
use Cool::Bar;
Cool::Foo::perl_rules();
Cool::Bar::perl_rules();
ok 1;
This tip has been brought to you by Nick Ing-Simmons and Stas Bekman.