2 use File::Basename qw(&basename &dirname);
8 my $file = basename($0, '.PL');
9 $file =~ s!_(pm)$!.$1!i;
14 my $Config_inc_version_list;
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.
20 my $expand_config_vars = 0;
21 if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) {
22 $expand_config_vars = 1;
24 elsif (exists $ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) {
25 $expand_config_vars = 0;
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 while (defined($_ = <$fh>)) {
35 # crude heuristics to check that we were using Config
36 if (/^\s*$ConfigRegex/ || /^\s*eval.*$ConfigRegex/) {
37 $expand_config_vars = 0;
41 $expand_config_vars = 1;
43 $expand_config_vars = 0 if $@;
46 if ($expand_config_vars) {
48 $Config_archname = qq('$Config{archname}');
49 $Config_version = qq('$Config{version}');
50 my @Config_inc_version_list =
51 reverse split / /, $Config{inc_version_list};
52 $Config_inc_version_list =
53 @Config_inc_version_list ?
54 qq(@Config_inc_version_list) : q(());
56 $useConfig = 'use Config;';
57 $Config_archname = q($Config{archname});
58 $Config_version = q($Config{version});
59 $Config_inc_version_list =
60 q(reverse split / /, $Config{inc_version_list});
63 open OUT,">$file" or die "Can't create $file: $!";
65 print "Extracting $file (with variable substitutions)\n";
67 # In this section, perl variables will be expanded during extraction.
68 # You can use $Config{...} to use Configure variables.
70 print OUT <<"!GROK!THIS!";
73 # THIS FILE IS AUTOMATICALLY GENERATED FROM lib_pm.PL.
74 # ANY CHANGES TO THIS FILE WILL BE OVERWRITTEN BY THE NEXT PERL BUILD.
80 my \$archname = $Config_archname;
81 my \$version = $Config_version;
82 my \@inc_version_list = $Config_inc_version_list;
85 print OUT <<'!NO!SUBS!';
87 our @ORIG_INC = @INC; # take a handy copy of 'original' value
88 our $VERSION = '0.57';
89 my $Is_MacOS = $^O eq 'MacOS';
93 $Mac_FS = eval { require Mac::FileSpec::Unixish };
100 foreach (reverse @_) {
101 my $path = $_; # we'll be modifying it, so break the alias
104 Carp::carp("Empty compile time value given to use lib");
107 $path = _nativize($path);
109 if ($path !~ /\.par$/i && -e $path && ! -d _) {
111 Carp::carp("Parameter to use lib must be directory, not file");
113 unshift(@INC, $path);
114 # Add any previous version directories we found at configure time
115 foreach my $incver (@inc_version_list)
118 ? File::Spec->catdir( $path, $incver )
120 unshift(@INC, $dir) if -d $dir;
122 # Put a corresponding archlib directory in front of $path if it
123 # looks like $path has an archlib directory below it.
124 my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir)
126 unshift(@INC, $arch_dir) if -d $arch_auto_dir;
127 unshift(@INC, $version_dir) if -d $version_dir;
128 unshift(@INC, $version_arch_dir) if -d $version_arch_dir;
131 # remove trailing duplicates
132 @INC = grep { ++$names{$_} == 1 } @INC;
142 my $path = _nativize($_);
144 my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir)
147 ++$names{$arch_dir} if -d $arch_auto_dir;
148 ++$names{$version_dir} if -d $version_dir;
149 ++$names{$version_arch_dir} if -d $version_arch_dir;
152 # Remove ALL instances of each named directory.
153 @INC = grep { !exists $names{$_} } @INC;
159 my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir);
161 # we could use this for all platforms in the future, but leave it
162 # Mac-only for now, until there is more time for testing it.
164 $arch_auto_dir = File::Spec->catdir( $dir, $archname, 'auto' );
165 $arch_dir = File::Spec->catdir( $dir, $archname, );
166 $version_dir = File::Spec->catdir( $dir, $version );
167 $version_arch_dir = File::Spec->catdir( $dir, $version, $archname );
169 $arch_auto_dir = "$dir/$archname/auto";
170 $arch_dir = "$dir/$archname";
171 $version_dir = "$dir/$version";
172 $version_arch_dir = "$dir/$version/$archname";
174 return($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir);
180 if ($Is_MacOS && $Mac_FS && ! -d $dir) {
181 $dir = Mac::FileSpec::Unixish::nativize($dir);
182 $dir .= ":" unless $dir =~ /:$/;
193 lib - manipulate @INC at compile time
203 This is a small simple module which simplifies the manipulation of @INC
206 It is typically used to add extra directories to perl's search path so
207 that later C<use> or C<require> statements will find modules which are
208 not located on perl's default search path.
210 =head2 Adding directories to @INC
212 The parameters to C<use lib> are added to the start of the perl search
217 is I<almost> the same as saying
219 BEGIN { unshift(@INC, LIST) }
221 For each directory in LIST (called $dir here) the lib module also
222 checks to see if a directory called $dir/$archname/auto exists.
223 If so the $dir/$archname directory is assumed to be a corresponding
224 architecture specific directory and is added to @INC in front of $dir.
226 The current value of C<$archname> can be found with this command:
230 To avoid memory leaks, all trailing duplicate entries in @INC are
233 =head2 Deleting directories from @INC
235 You should normally only add directories to @INC. If you need to
236 delete directories from @INC take care to only delete those which you
237 added yourself or which you are certain are not needed by other modules
238 in your script. Other modules may have added directories which they
239 need for correct operation.
241 The C<no lib> statement deletes all instances of each named directory
244 For each directory in LIST (called $dir here) the lib module also
245 checks to see if a directory called $dir/$archname/auto exists.
246 If so the $dir/$archname directory is assumed to be a corresponding
247 architecture specific directory and is also deleted from @INC.
249 =head2 Restoring original @INC
251 When the lib module is first loaded it records the current value of @INC
252 in an array C<@lib::ORIG_INC>. To restore @INC to that value you
255 @INC = @lib::ORIG_INC;
259 In order to keep lib.pm small and simple, it only works with Unix
260 filepaths. This doesn't mean it only works on Unix, but non-Unix
261 users must first translate their file paths to Unix conventions.
263 # VMS users wanting to put [.stuff.moo] into
264 # their @INC would write
269 In the future, this module will likely use File::Spec for determining
270 paths, as it does now for Mac OS (where Unix-style or Mac-style paths
271 work, and Unix-style paths are converted properly to Mac-style paths
272 before being added to @INC).
274 If you try to add a file to @INC as follows:
276 use lib 'this_is_a_file.txt';
278 C<lib> will warn about this. The sole exceptions are files with the
279 C<.par> extension which are intended to be used as libraries.
283 FindBin - optional module which deals with paths relative to the source file.
285 PAR - optional module which can treat C<.par> files as Perl libraries.
289 Tim Bunce, 2nd June 1995.
291 C<lib> is maintained by the perl5-porters. Please direct
292 any questions to the canonical mailing list. Anything that
293 is applicable to the CPAN release can be sent to its maintainer,
296 Maintainer: The Perl5-Porters <perl5-porters@perl.org>
298 Maintainer of the CPAN release: Steffen Mueller <smueller@cpan.org>
300 =head1 COPYRIGHT AND LICENSE
302 This package has been part of the perl core since perl 5.001.
303 It has been released separately to CPAN so older installations
304 can benefit from bug fixes.
306 This package has the same copyright and license as the perl core.
311 close OUT or die "Can't close $file: $!";