From: t0m Date: Wed, 2 Sep 2009 22:35:37 +0000 (+0000) Subject: De-dup @INC and $ENV{PERL5LIB} entries X-Git-Tag: 1.006009~63 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2Flocal-lib.git;a=commitdiff_plain;h=a9489cb038b7412d3f5c3f7c4cc18d2701b9a5e2 De-dup @INC and $ENV{PERL5LIB} entries git-svn-id: http://dev.catalyst.perl.org/repos/bast/local-lib/1.000/trunk@7495 bd8105ee-0ff8-0310-8827-fb3f25b6796d --- diff --git a/Changes b/Changes index 7735455..81f3367 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for local::lib + - Ensure that $ENV{PERL5LIB} and @INC are always de-duped, stopping them + from growing if local::lib is invoked multiple times for the same path. + 1.004006 2009-08-25 - Fix parsing of --self-contained and local lib directory. It's now possible to specify flags and the directory in any order. Also made diff --git a/lib/local/lib.pm b/lib/local/lib.pm index 889f28b..dc5ea54 100644 --- a/lib/local/lib.pm +++ b/lib/local/lib.pm @@ -53,17 +53,17 @@ DEATH # The only directories that remain are those that we just defined and those # where core modules are stored. We put PERL5LIB first, so it'll be favored # over privlibexp and archlibexp - my %seen; - @INC = grep { ! $seen{$_}++ } ( + + @INC = _uniq( $class->install_base_perl_path($arg_store{path}), $class->install_base_arch_path($arg_store{path}), split( $Config{path_sep}, $perl5lib ), $Config::Config{privlibexp}, $Config::Config{archlibexp} - ); - + ); + # We explicitly set PERL5LIB here to the above de-duped list to prevent - # @INC from growing with each invocation + # @INC from growing with each invocation $ENV{PERL5LIB} = join( $Config{path_sep}, @INC ); } @@ -109,6 +109,11 @@ Test::More::ok($foo->${pipeline qw(foo bar baz)}(10) == -15); =cut +sub _uniq { + my %seen; + grep { ! $seen{$_}++ } @_; +} + sub resolve_path { my ($class, $path) = @_; $class->${pipeline qw( @@ -206,7 +211,7 @@ sub setup_local_lib_for { exit 0; } else { $class->setup_env_hash_for($path); - unshift(@INC, split($Config{path_sep}, $ENV{PERL5LIB})); + @INC = _uniq(split($Config{path_sep}, $ENV{PERL5LIB}), @INC); } } diff --git a/t/de-dup.t b/t/de-dup.t new file mode 100644 index 0000000..fdc325b --- /dev/null +++ b/t/de-dup.t @@ -0,0 +1,34 @@ +use strict; +use warnings; +use Test::More; +use File::Temp qw(tempdir); +use Cwd; + +plan tests => 4; + +my $dir = tempdir('test_local_lib-XXXXX', DIR => Cwd::abs_path('t'), CLEANUP => 1); + +use local::lib (); +local::lib->import($dir); +local::lib->import($dir); + +{ + my (%inc, %perl5lib); + map { $inc{$_}++ } @INC; + map { $perl5lib{$_} } split /:/, $ENV{PERL5LIB}; + ok ! grep({ $inc{$_} > 1 } keys %inc), '@INC entries not duplicated'; +use Data::Dumper; +warn Dumper(\@INC); + ok ! grep({ $perl5lib{$_} > 1 } keys %perl5lib), 'ENV{PERL5LIB} entries not duplicated'; +} + +local::lib->import('--self-contained', $dir); + +{ + my (%inc, %perl5lib); + map { $inc{$_}++ } @INC; + map { $perl5lib{$_} } split /:/, $ENV{PERL5LIB}; + ok ! grep({ $inc{$_} > 1 } keys %inc), '@INC entries not duplicated (--self-contained)'; + ok ! grep({ $perl5lib{$_} > 1 } keys %perl5lib), 'ENV{PERL5LIB} entries not duplicated (--self-contained)'; +} +