6 use 5.008001; # probably works with earlier versions but I'm not supporting them
7 # (patches would, of course, be welcome)
14 our $VERSION = '1.003004'; # 1.3.4
17 my ($class, @args) = @_;
19 # The path is required, but last in the list, so we pop, not shift here.
21 $path = $class->resolve_path($path);
22 $class->setup_local_lib_for($path);
24 # Handle the '--self-contained' option
25 my $flag = shift @args;
26 no warnings 'uninitialized'; # the flag is optional
27 # make sure fancy dashes cause an error
30 WHOA THERE! It looks like you've got some fancy dashes in your commandline!
31 These are *not* the traditional -- dashes that software recognizes. You
32 probably got these by copy-pasting from the perldoc for this module as
33 rendered by a UTF8-capable formatter. This most typically happens on an OS X
34 terminal, but can happen elsewhere too. Please try again after replacing the
35 dashes with normal minus signs.
38 if ($flag eq '--self-contained') {
39 # The only directories that remain are those that we just defined and those where core modules are stored.
40 @INC = ($Config::Config{privlibexp}, $Config::Config{archlibexp}, split ':', $ENV{PERL5LIB});
42 elsif (defined $flag) {
43 die "unrecognized import argument: $flag";
52 my $last = pop(@methods);
55 my ($obj, @args) = @_;
56 $obj->${pipeline @methods}(
73 { package Foo; sub foo { -$_[1] } sub bar { $_[1]+2 } sub baz { $_[1]+3 } }
74 my $foo = bless({}, 'Foo');
75 Test::More::ok($foo->${pipeline qw(foo bar baz)}(10) == -15);
82 my ($class, $path) = @_;
83 $class->${pipeline qw(
90 sub resolve_empty_path {
91 my ($class, $path) = @_;
101 #:: test classmethod setup
103 my $c = 'local::lib';
111 is($c->resolve_empty_path, '~/perl5');
112 is($c->resolve_empty_path('foo'), 'foo');
118 sub resolve_home_path {
119 my ($class, $path) = @_;
120 return $path unless ($path =~ /^~/);
121 my ($user) = ($path =~ /^~([^\/]+)/); # can assume ^~ so undef for 'us'
122 my $tried_file_homedir;
124 if (eval { require File::HomeDir } && $File::HomeDir::VERSION >= 0.65) {
125 $tried_file_homedir = 1;
127 File::HomeDir->users_home($user);
129 File::HomeDir->my_home;
135 if (defined $ENV{HOME}) {
143 unless (defined $homedir) {
145 "Couldn't resolve homedir for "
146 .(defined $user ? $user : 'current user')
147 .($tried_file_homedir ? '' : ' - consider installing File::HomeDir')
150 $path =~ s/^~[^\/]*/$homedir/;
154 sub resolve_relative_path {
155 my ($class, $path) = @_;
156 File::Spec->rel2abs($path);
163 local *File::Spec::rel2abs = sub { shift; 'FOO'.shift; };
164 is($c->resolve_relative_path('bar'),'FOObar');
170 sub setup_local_lib_for {
171 my ($class, $path) = @_;
172 $class->ensure_dir_structure_for($path);
174 $class->print_environment_vars_for($path);
177 $class->setup_env_hash_for($path);
178 unshift(@INC, split(':', $ENV{PERL5LIB}));
182 sub modulebuildrc_path {
183 my ($class, $path) = @_;
184 File::Spec->catfile($path, '.modulebuildrc');
187 sub install_base_bin_path {
188 my ($class, $path) = @_;
189 File::Spec->catdir($path, 'bin');
192 sub install_base_perl_path {
193 my ($class, $path) = @_;
194 File::Spec->catdir($path, 'lib', 'perl5');
197 sub install_base_arch_path {
198 my ($class, $path) = @_;
199 File::Spec->catdir($class->install_base_perl_path($path), $Config{archname});
202 sub ensure_dir_structure_for {
203 my ($class, $path) = @_;
205 warn "Attempting to create directory ${path}\n";
207 File::Path::mkpath($path);
208 my $modulebuildrc_path = $class->modulebuildrc_path($path);
209 if (-e $modulebuildrc_path) {
211 Carp::croak("${modulebuildrc_path} exists but is not a plain file");
214 warn "Attempting to create file ${modulebuildrc_path}\n";
215 open MODULEBUILDRC, '>', $modulebuildrc_path
216 || Carp::croak("Couldn't open ${modulebuildrc_path} for writing: $!");
217 print MODULEBUILDRC qq{install --install_base ${path}\n}
218 || Carp::croak("Couldn't write line to ${modulebuildrc_path}: $!");
220 || Carp::croak("Couldn't close file ${modulebuildrc_path}: $@");
224 sub INTERPOLATE_ENV () { 1 }
225 sub LITERAL_ENV () { 0 }
227 sub print_environment_vars_for {
228 my ($class, $path) = @_;
229 my @envs = $class->build_environment_vars_for($path, LITERAL_ENV);
232 # rather basic csh detection, goes on the assumption that something won't
233 # call itself csh unless it really is. also, default to bourne in the
234 # pathological situation where a user doesn't have $ENV{SHELL} defined.
235 # note also that shells with funny names, like zoid, are assumed to be
238 if(defined $ENV{'SHELL'}) {
239 my @shell_bin_path_parts = File::Spec->splitpath($ENV{'SHELL'});
240 $shellbin = $shell_bin_path_parts[-1];
243 local $_ = $shellbin;
252 my ($name, $value) = (shift(@envs), shift(@envs));
253 $value =~ s/(\\")/\\$1/g;
254 $out .= $class->${\"build_${shelltype}_env_declaration"}($name, $value);
259 # simple routines that take two arguments: an %ENV key and a value. return
260 # strings that are suitable for passing directly to the relevant shell to set
261 # said key to said value.
262 sub build_bourne_env_declaration {
264 my($name, $value) = @_;
265 return qq{export ${name}="${value}"\n};
268 sub build_csh_env_declaration {
270 my($name, $value) = @_;
271 return qq{setenv ${name} "${value}"\n};
274 sub setup_env_hash_for {
275 my ($class, $path) = @_;
276 my %envs = $class->build_environment_vars_for($path, INTERPOLATE_ENV);
277 @ENV{keys %envs} = values %envs;
280 sub build_environment_vars_for {
281 my ($class, $path, $interpolate) = @_;
283 MODULEBUILDRC => $class->modulebuildrc_path($path),
284 PERL_MM_OPT => "INSTALL_BASE=${path}",
285 PERL5LIB => join(':',
286 $class->install_base_perl_path($path),
287 $class->install_base_arch_path($path),
289 ($interpolate == INTERPOLATE_ENV
295 $class->install_base_bin_path($path),
296 ($interpolate == INTERPOLATE_ENV
307 File::Path::rmtree('t/var/splat');
309 $c->ensure_dir_structure_for('t/var/splat');
311 ok(-d 't/var/splat');
313 ok(-f 't/var/splat/.modulebuildrc');
319 local::lib - create and use a local lib/ for perl modules with PERL5LIB
325 use local::lib; # sets up a local lib at ~/perl5
327 use local::lib '~/foo'; # same, but ~/foo
331 use local::lib "$FindBin::Bin/../support"; # app-local support library
335 # Install LWP and it's missing dependencies to the 'my_lwp' directory
336 perl -MCPAN -Mlocal::lib=my_lwp -e 'CPAN::install(LWP)'
338 # Install LWP and *all non-core* dependencies to the 'my_lwp' directory
339 perl -MCPAN -Mlocal::lib=--self-contained,my_lwp -e 'CPAN::install(LWP)'
341 # Just print out useful shell commands
343 export MODULEBUILDRC=/home/username/perl/.modulebuildrc
344 export PERL_MM_OPT='INSTALL_BASE=/home/username/perl'
345 export PERL5LIB='/home/username/perl/lib/perl5:/home/username/perl/lib/perl5/i386-linux'
346 export PATH="/home/username/perl/bin:$PATH"
348 To bootstrap if you don't have local::lib itself installed -
350 <download local::lib tarball from CPAN, unpack and cd into dir>
352 $ perl Makefile.PL --bootstrap
353 $ make test && make install
355 $ echo 'eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)' >>~/.bashrc
362 % perl -I$HOME/perl5/lib/perl5 -Mlocal::lib >> ~/.cshrc
364 You can also pass --boostrap=~/foo to get a different location -
366 $ perl Makefile.PL --bootstrap=~/foo
367 $ make test && make install
369 $ echo 'eval $(perl -I$HOME/foo/lib/perl5 -Mlocal::lib=$HOME/foo)' >>~/.bashrc
371 If you want to install multiple Perl module environments, say for application evelopment,
372 install local::lib globally and then:
375 $ perl -Mlocal::lib=./
376 $ eval $(perl -Mlocal::lib=./) ### To set the environment for this shell alone
377 $ printenv ### You will see that ~/mydir1 is in the PERL5LIB
378 $ perl -MCPAN -e install ... ### whatever modules you want
382 For multiple environments for multiple apps you may need to include a modified version of
383 the C<< use FindBin >> instructions in the "In code" sample above. If you did something like
384 the above, you have a set of Perl modules at C<< ~/mydir1/lib >>. If you have a script at
385 C<< ~/mydir1/scripts/myscript.pl >>, you need to tell it where to find the modules you installed
386 for it at C<< ~/mydir1/lib>>.
388 In C<< ~/mydir1/scripts/myscript.pl >>:
392 use local::lib "$FindBin::Bin/.."; ### points to ~/mydir1 and local::lib finds lib
393 use lib "$FindBin::Bin/../lib"; ### points to ~/mydir1/lib
395 Put this before any BEGIN { ... } blocks that require the modules you installed.
399 This module provides a quick, convenient way of bootstrapping a user-local Perl
400 module library located within the user's home directory. It also constructs and
401 prints out for the user the list of environment variables using the syntax
402 appropriate for the user's current shell (as specified by the C<SHELL>
403 environment variable), suitable for directly adding to one's shell configuration
406 More generally, local::lib allows for the bootstrapping and usage of a directory
407 containing Perl modules outside of Perl's C<@INC>. This makes it easier to ship
408 an application with an app-specific copy of a Perl module, or collection of
409 modules. Useful in cases like when an upstream maintainer hasn't applied a patch
410 to a module of theirs that you need for your application.
412 On import, local::lib sets the following environment variables to appropriate
425 PATH is appended to, rather than clobbered.
429 These values are then available for reference by any code after import.
433 =head2 ensure_directory_structure_for
437 =item Arguments: path
441 Attempts to create the given path, and all required parent directories. Throws
442 an exception on failure.
444 =head2 print_environment_vars_for
448 =item Arguments: path
452 Prints to standard output the variables listed above, properly set to use the
453 given path as the base directory.
455 =head2 setup_env_hash_for
459 =item Arguments: path
463 Constructs the C<%ENV> keys for the given path, by calling
464 C<build_environment_vars_for>.
466 =head2 install_base_perl_path
470 =item Arguments: path
474 Returns a path describing where to install the Perl modules for this local
475 library installation. Appends the directories C<lib> and C<perl5> to the given
478 =head2 install_base_arch_path
482 =item Arguments: path
486 Returns a path describing where to install the architecture-specific Perl
487 modules for this local library installation. Based on the
488 L</install_base_perl_path> method's return value, and appends the value of
489 C<$Config{archname}>.
491 =head2 install_base_bin_path
495 =item Arguments: path
499 Returns a path describing where to install the executable programs for this
500 local library installation. Based on the L</install_base_perl_path> method's
501 return value, and appends the directory C<bin>.
503 =head2 modulebuildrc_path
507 =item Arguments: path
511 Returns a path describing where to install the C<.modulebuildrc> file, based on
514 =head2 resolve_empty_path
518 =item Arguments: path
522 Builds and returns the base path into which to set up the local module
523 installation. Defaults to C<~/perl5>.
525 =head2 resolve_home_path
529 =item Arguments: path
533 Attempts to find the user's home directory. If installed, uses C<File::HomeDir>
534 for this purpose. If no definite answer is available, throws an exception.
536 =head2 resolve_relative_path
540 =item Arguments: path
544 Translates the given path into an absolute path.
550 =item Arguments: path
554 Calls the following in a pipeline, passing the result from the previous to the
555 next, in an attempt to find where to configure the environment for a local
556 library installation: L</resolve_empty_path>, L</resolve_home_path>,
557 L</resolve_relative_path>. Passes the given path argument to
558 L</resolve_empty_path> which then returns a result that is passed to
559 L</resolve_home_path>, which then has its result passed to
560 L</resolve_relative_path>. The result of this final call is returned from
563 =head1 A WARNING ABOUT UNINST=1
565 Be careful about using local::lib in combination with "make install UNINST=1".
566 The idea of this feature is that will uninstall an old version of a module
567 before installing a new one. However it lacks a safety check that the old
568 version and the new version will go in the same directory. Used in combination
569 with local::lib, you can potentially delete a globally accessible version of a
570 module while installing the new version in a local place. Only combine "make
571 install UNINST=1" and local::lib if you understand these possible consequences.
575 Rather basic shell detection. Right now anything with csh in its name is
576 assumed to be a C shell or something compatible, and everything else is assumed
577 to be Bourne. If the C<SHELL> environment variable is not set, a
578 Bourne-compatible shell is assumed.
580 Bootstrap is a hack and will use CPAN.pm for ExtUtils::MakeMaker even if you
581 have CPANPLUS installed.
583 Kills any existing PERL5LIB, PERL_MM_OPT or MODULEBUILDRC.
585 Should probably auto-fixup CPAN config if not already done.
587 Patches very much welcome for any of the above.
595 local::lib looks at the user's C<SHELL> environment variable when printing out
596 commands to add to the shell configuration file.
602 Matt S Trout <mst@shadowcat.co.uk> http://www.shadowcat.co.uk/
604 auto_install fixes kindly sponsored by http://www.takkle.com/
608 Patches to correctly output commands for csh style shells, as well as some
609 documentation additions, contributed by Christopher Nehren <apeiron@cpan.org>.
611 '--self-contained' feature contributed by Mark Stosberg <mark@summersault.com>.
613 Doc patches for a custom local::lib patch contributed by Torsten Raudssus
614 <torsten@raudssus.de>.
616 Hans Dieter Pearcey <hdp@cpan.org> sent in some additional tests for ensuring
617 things will install properly.
621 This library is free software under the same license as perl itself