X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Flocal%2Flib.pm;h=355278e5266a56b06178079b0d9b3677556624f2;hb=618272fe8c2729ed2ce0bdeb13d4476a6b52f6a1;hp=557a9a6063af6af1bf04c432bdffd1af3746b176;hpb=b5cc15f7139b20feea57c18e5498ef0dd1432935;p=p5sagit%2Flocal-lib.git diff --git a/lib/local/lib.pm b/lib/local/lib.pm index 557a9a6..355278e 100644 --- a/lib/local/lib.pm +++ b/lib/local/lib.pm @@ -19,15 +19,15 @@ sub import { $class->setup_local_lib_for($path); } -sub compose; +sub pipeline; -sub compose { +sub pipeline { my @methods = @_; my $last = pop(@methods); if (@methods) { \sub { my ($obj, @args) = @_; - $obj->${compose @methods}( + $obj->${pipeline @methods}( $obj->$last(@args) ); }; @@ -38,19 +38,19 @@ sub compose { } } -=for test +=for test pipeline package local::lib; { package Foo; sub foo { -$_[1] } sub bar { $_[1]+2 } sub baz { $_[1]+3 } } my $foo = bless({}, 'Foo'); -ok($foo->${compose qw(foo bar baz)}(10) == -15); +Test::More::ok($foo->${pipeline qw(foo bar baz)}(10) == -15); =cut sub resolve_path { my ($class, $path) = @_; - $class->${compose qw( + $class->${pipeline qw( resolve_relative_path resolve_home_path resolve_empty_path @@ -90,7 +90,7 @@ sub resolve_home_path { if (defined $user) { File::HomeDir->users_home($user); } else { - File::HomeDir->my_homedir; + File::HomeDir->my_home; } } else { if (defined $user) { @@ -135,6 +135,7 @@ sub setup_local_lib_for { exit 0; } else { $class->setup_env_hash_for($path); + unshift(@INC, split(':', $ENV{PERL5LIB})); } } @@ -173,7 +174,7 @@ sub ensure_dir_structure_for { warn "Attempting to create file ${modulebuildrc_path}\n"; open MODULEBUILDRC, '>', $modulebuildrc_path || Carp::croak("Couldn't open ${modulebuildrc_path} for writing: $!"); - print MODULEBUILDRC qq{--install_base ${path}\n} + print MODULEBUILDRC qq{install --install_base ${path}\n} || Carp::croak("Couldn't write line to ${modulebuildrc_path}: $!"); close MODULEBUILDRC || Carp::croak("Couldn't close file ${modulebuildrc_path}: $@"); @@ -190,7 +191,23 @@ sub print_environment_vars_for { while (@envs) { my ($name, $value) = (shift(@envs), shift(@envs)); $value =~ s/(\\")/\\$1/g; - $out .= qq{export ${name}="${value}"\n}; + + # rather basic csh detection, goes on the assumption that something won't + # call itself csh unless it really is. also, default to bourne in the + # pathological situation where a user doesn't have $ENV{SHELL} defined. + # note also that shells with funny names, like zoid, are assumed to be + # bourne. + my $shellbin = 'sh'; + if(defined $ENV{'SHELL'}) { + my @shell_bin_path_parts = File::Spec->splitpath($ENV{'SHELL'}); + $shellbin = $shell_bin_path_parts[-1]; + } + if($shellbin =~ /csh/) { + $out .= qq{setenv ${name} "${value}"\n}; + } + else { + $out .= qq{export ${name}="${value}"\n}; + } } print $out; } @@ -223,7 +240,7 @@ sub build_environment_vars_for { File::Path::rmtree('t/var/splat'); -$c->resolve_relative_path('t/var/splat'); +$c->ensure_dir_structure_for('t/var/splat'); ok(-d 't/var/splat'); @@ -249,6 +266,58 @@ From the shell - export PERL5LIB='/home/username/perl/lib/perl5:/home/username/perl/lib/perl5/i386-linux' export PATH="/home/username/perl/bin:$PATH" +To bootstrap if you don't have local::lib itself installed - + + $ perl -MCPAN -eshell # you only need to do this if you don't have a ~/.cpan + cpan> exit + + $ perl Makefile.PL --bootstrap + $ make test && make install + $ echo 'eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)' >>~/.bashrc + # Or for C shells... + $ /bin/csh + % echo $SHELL + /bin/csh + % perl -I$HOME/perl5/lib/perl5 -Mlocal::lib >> ~/.cshrc + +You can also pass --boostrap=~/foo to get a different location (adjust the +bashrc / cshrc line appropriately) + +=head1 DESCRIPTION + +This module provides a quick, convenient way of bootstrapping a user-local Perl +module library located within the user's home directory. It also constructs and +prints out for the user the list of environment variables using the syntax +appropriate for the user's current shell (as specified by the C +environment variable), suitable for directly adding to one's shell configuration +file. + +=head1 LIMITATIONS + +Rather basic shell detection. Right now anything with csh in its name is +assumed to be a C shell or something compatible, and everything else is assumed +to be Bourne. + +Bootstrap is a hack and will use CPAN.pm for ExtUtils::MakeMaker even if you +have CPANPLUS installed. + +Kills any existing PERL5LIB, PERL_MM_OPT or MODULEBUILDRC. + +Should probably auto-fixup CPAN config if not already done. + +Patches very much welcome for any of the above. + +=head1 ENVIRONMENT + +=over 4 + +=item SHELL + +local::lib looks at the user's C environment variable when printing out +commands to add to the shell configuration file. + +=back + =head1 AUTHOR Matt S Trout http://www.shadowcat.co.uk/