1use Config; 2use File::Basename qw(&basename &dirname); 3use File::Spec; 4use Cwd; 5 6my $origdir = cwd; 7chdir dirname($0); 8my $file = basename($0, '.PL'); 9$file =~ s/_(pm)$/.$1/i; 10 11my $useConfig; 12my $Config_archname; 13my $Config_version; 14my $Config_inc_version_list; 15 16# Expand the variables only if explicitly requested 17# or if a previously installed lib.pm does this, too 18# because otherwise relocating Perl becomes much harder. 19 20my $expand_config_vars = 0; 21if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) { 22 $expand_config_vars = 1; 23} 24elsif (exists $ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) { 25 $expand_config_vars = 0; 26} 27else { 28 eval <<'HERE'; 29 require lib; 30 my $lib_file = $INC{"lib.pm"}; 31 open my $fh, '<', $lib_file 32 or die "Could not open file '$lib_file' for reading: $!"; 33 my $ConfigRegex = qr/(?:use|require)\s+Config(?:\s+|;)/; 34 my $found_config = 0; 35 while (defined($_ = <$fh>)) { 36 # crude heuristics to check that we were using Config 37 if (/^\s*$ConfigRegex/ || /^\s*eval.*$ConfigRegex/) { 38 $found_config = 1; 39 last; 40 } 41 } 42 $expand_config_vars = $found_config ? 0 : 1; 43HERE 44 $expand_config_vars = 0 if $@; 45} 46 47if ($expand_config_vars) { 48 $useConfig = ''; 49 $Config_archname = qq('$Config{archname}'); 50 $Config_version = qq('$Config{version}'); 51 my @Config_inc_version_list = 52 reverse split / /, $Config{inc_version_list}; 53 $Config_inc_version_list = 54 @Config_inc_version_list ? 55 qq(qw(@Config_inc_version_list)) : q(()); 56} else { 57 $useConfig = 'use Config;'; 58 $Config_archname = q($Config{archname}); 59 $Config_version = q($Config{version}); 60 $Config_inc_version_list = 61 q(reverse split / /, $Config{inc_version_list}); 62} 63 64open OUT,'>', $file or die "Can't create $file: $!"; 65 66print "Extracting $file (with variable substitutions)\n"; 67 68# In this section, perl variables will be expanded during extraction. 69# You can use $Config{...} to use Configure variables. 70 71print OUT <<"!GROK!THIS!"; 72package lib; 73 74# THIS FILE IS AUTOMATICALLY GENERATED FROM lib_pm.PL. 75# ANY CHANGES TO THIS FILE WILL BE OVERWRITTEN BY THE NEXT PERL BUILD. 76 77$useConfig 78 79use strict; 80 81my \$archname = $Config_archname; 82my \$version = $Config_version; 83my \@inc_version_list = $Config_inc_version_list; 84 85!GROK!THIS! 86print OUT <<'!NO!SUBS!'; 87 88our @ORIG_INC = @INC; # take a handy copy of 'original' value 89our $VERSION = '0.65'; 90 91sub import { 92 shift; 93 94 my %names; 95 foreach (reverse @_) { 96 my $path = $_; # we'll be modifying it, so break the alias 97 if ($path eq '') { 98 require Carp; 99 Carp::carp("Empty compile time value given to use lib"); 100 } 101 102 if ($path !~ /\.par$/i && -e $path && ! -d _) { 103 require Carp; 104 Carp::carp("Parameter to use lib must be directory, not file"); 105 } 106 unshift(@INC, $path); 107 # Add any previous version directories we found at configure time 108 foreach my $incver (@inc_version_list) 109 { 110 my $dir = "$path/$incver"; 111 unshift(@INC, $dir) if -d $dir; 112 } 113 # Put a corresponding archlib directory in front of $path if it 114 # looks like $path has an archlib directory below it. 115 my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir) 116 = _get_dirs($path); 117 unshift(@INC, $arch_dir) if -d $arch_auto_dir; 118 unshift(@INC, $version_dir) if -d $version_dir; 119 unshift(@INC, $version_arch_dir) if -d $version_arch_dir; 120 } 121 122 # remove trailing duplicates 123 @INC = grep { ++$names{$_} == 1 } @INC; 124 return; 125} 126 127 128sub unimport { 129 shift; 130 131 my %names; 132 foreach my $path (@_) { 133 my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir) 134 = _get_dirs($path); 135 ++$names{$path}; 136 ++$names{$arch_dir} if -d $arch_auto_dir; 137 ++$names{$version_dir} if -d $version_dir; 138 ++$names{$version_arch_dir} if -d $version_arch_dir; 139 } 140 141 # Remove ALL instances of each named directory. 142 @INC = grep { !exists $names{$_} } @INC; 143 return; 144} 145 146sub _get_dirs { 147 my($dir) = @_; 148 my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir); 149 150 $arch_auto_dir = "$dir/$archname/auto"; 151 $arch_dir = "$dir/$archname"; 152 $version_dir = "$dir/$version"; 153 $version_arch_dir = "$dir/$version/$archname"; 154 155 return($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir); 156} 157 1581; 159__END__ 160 161=head1 NAME 162 163lib - manipulate @INC at compile time 164 165=head1 SYNOPSIS 166 167 use lib LIST; 168 169 no lib LIST; 170 171=head1 DESCRIPTION 172 173This is a small simple module which simplifies the manipulation of @INC 174at compile time. 175 176It is typically used to add extra directories to perl's search path so 177that later C<use> or C<require> statements will find modules which are 178not located on perl's default search path. 179 180=head2 Adding directories to @INC 181 182The parameters to C<use lib> are added to the start of the perl search 183path. Saying 184 185 use lib LIST; 186 187is I<almost> the same as saying 188 189 BEGIN { unshift(@INC, LIST) } 190 191For each directory in LIST (called $dir here) the lib module also 192checks to see if a directory called $dir/$archname/auto exists. 193If so the $dir/$archname directory is assumed to be a corresponding 194architecture specific directory and is added to @INC in front of $dir. 195lib.pm also checks if directories called $dir/$version and $dir/$version/$archname 196exist and adds these directories to @INC. 197 198The current value of C<$archname> can be found with this command: 199 200 perl -V:archname 201 202The corresponding command to get the current value of C<$version> is: 203 204 perl -V:version 205 206To avoid memory leaks, all trailing duplicate entries in @INC are 207removed. 208 209=head2 Deleting directories from @INC 210 211You should normally only add directories to @INC. If you need to 212delete directories from @INC take care to only delete those which you 213added yourself or which you are certain are not needed by other modules 214in your script. Other modules may have added directories which they 215need for correct operation. 216 217The C<no lib> statement deletes all instances of each named directory 218from @INC. 219 220For each directory in LIST (called $dir here) the lib module also 221checks to see if a directory called $dir/$archname/auto exists. 222If so the $dir/$archname directory is assumed to be a corresponding 223architecture specific directory and is also deleted from @INC. lib.pm also 224checks if directories called $dir/$version and $dir/$version/$archname 225exist and deletes these directories from @INC. 226 227=head2 Restoring original @INC 228 229When the lib module is first loaded it records the current value of @INC 230in an array C<@lib::ORIG_INC>. To restore @INC to that value you 231can say 232 233 @INC = @lib::ORIG_INC; 234 235=head1 CAVEATS 236 237In order to keep lib.pm small and simple, it only works with Unix 238filepaths. This doesn't mean it only works on Unix, but non-Unix 239users must first translate their file paths to Unix conventions. 240 241 # VMS users wanting to put [.stuff.moo] into 242 # their @INC would write 243 use lib 'stuff/moo'; 244 245=head1 NOTES 246 247In the future, this module will likely use File::Spec for determining 248paths, as it does now for Mac OS (where Unix-style or Mac-style paths 249work, and Unix-style paths are converted properly to Mac-style paths 250before being added to @INC). 251 252If you try to add a file to @INC as follows: 253 254 use lib 'this_is_a_file.txt'; 255 256C<lib> will warn about this. The sole exceptions are files with the 257C<.par> extension which are intended to be used as libraries. 258 259=head1 SEE ALSO 260 261FindBin - optional module which deals with paths relative to the source file. 262 263PAR - optional module which can treat C<.par> files as Perl libraries. 264 265=head1 AUTHOR 266 267Tim Bunce, 2nd June 1995. 268 269C<lib> is maintained by the perl5-porters. Please direct 270any questions to the canonical mailing list. Anything that 271is applicable to the CPAN release can be sent to its maintainer, 272though. 273 274Maintainer: The Perl5-Porters <perl5-porters@perl.org> 275 276Maintainer of the CPAN release: Steffen Mueller <smueller@cpan.org> 277 278=head1 COPYRIGHT AND LICENSE 279 280This package has been part of the perl core since perl 5.001. 281It has been released separately to CPAN so older installations 282can benefit from bug fixes. 283 284This package has the same copyright and license as the perl core. 285 286=cut 287!NO!SUBS! 288 289close OUT or die "Can't close $file: $!"; 290chdir $origdir; 291