Can't get #17492 to work with -Uuseperlio otherwise (either
[p5sagit/p5-mst-13.2.git] / lib / lib_pm.PL
CommitLineData
60ed1d8c 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');
4755096e 9$file =~ s!_(pm)$!.$1!i;
60ed1d8c 10
e9c6cca7 11my $useConfig;
12my $Config_archname;
13my $Config_version;
14my $Config_inc_version_list;
15
16# Expand the variables only if explicitly requested because
17# otherwise relocating Perl becomes much harder.
18
19if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) {
20 $useConfig = '';
21 $Config_archname = qq('$Config{archname}');
22 $Config_version = qq('$Config{version}');
23 my @Config_inc_version_list =
24 reverse split / /, $Config{inc_version_list};
25 $Config_inc_version_list =
26 @Config_inc_version_list ?
27 qq(@Config_inc_version_list) : q(());
28} else {
29 $useConfig = 'use Config;';
30 $Config_archname = q($Config{archname});
31 $Config_version = q($Config{version});
32 $Config_inc_version_list =
91a12f7d 33 q(reverse split / /, $Config{inc_version_list});
e9c6cca7 34}
60ed1d8c 35
36open OUT,">$file" or die "Can't create $file: $!";
37
38print "Extracting $file (with variable substitutions)\n";
39
40# In this section, perl variables will be expanded during extraction.
41# You can use $Config{...} to use Configure variables.
42
43print OUT <<"!GROK!THIS!";
e50aee73 44package lib;
45
427f4adb 46# THIS FILE IS AUTOMATICALLY GENERATED FROM lib_pm.PL.
47# ANY CHANGES TO THIS FILE WILL BE OVERWRITTEN BY THE NEXT PERL BUILD.
4633a7c4 48
e9c6cca7 49$useConfig
50
fbc36ba3 51use strict;
52
e9c6cca7 53my \$archname = $Config_archname;
54my \$version = $Config_version;
55my \@inc_version_list = $Config_inc_version_list;
60ed1d8c 56
57!GROK!THIS!
58print OUT <<'!NO!SUBS!';
4633a7c4 59
17f410f9 60our @ORIG_INC = @INC; # take a handy copy of 'original' value
61our $VERSION = '0.5564';
d5201bd2 62my $Is_MacOS = $^O eq 'MacOS';
63my $Mac_FS;
64if ($Is_MacOS) {
65 require File::Spec;
66 $Mac_FS = eval { require Mac::FileSpec::Unixish };
67}
e50aee73 68
69sub import {
70 shift;
aeb5d71d 71
72 my %names;
a5f75d66 73 foreach (reverse @_) {
016609bc 74 if ($_ eq '') {
af3dad46 75 require Carp;
774d564b 76 Carp::carp("Empty compile time value given to use lib");
af3dad46 77 }
d5201bd2 78
79 local $_ = _nativize($_);
80
20408e3c 81 if (-e && ! -d _) {
82 require Carp;
83 Carp::carp("Parameter to use lib must be directory, not file");
84 }
4633a7c4 85 unshift(@INC, $_);
d5201bd2 86 # Add any previous version directories we found at configure time
e438405d 87 foreach my $incver (@inc_version_list)
d5201bd2 88 {
89 my $dir = $Is_MacOS
90 ? File::Spec->catdir( $_, $incver )
91 : "$_/$incver";
92 unshift(@INC, $dir) if -d $dir;
93 }
e438405d 94 # Put a corresponding archlib directory in front of $_ if it
4633a7c4 95 # looks like $_ has an archlib directory below it.
d5201bd2 96 my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir)
97 = _get_dirs($_);
98 unshift(@INC, $arch_dir) if -d $arch_auto_dir;
99 unshift(@INC, $version_dir) if -d $version_dir;
100 unshift(@INC, $version_arch_dir) if -d $version_arch_dir;
4633a7c4 101 }
abef537a 102
103 # remove trailing duplicates
104 @INC = grep { ++$names{$_} == 1 } @INC;
105 return;
e50aee73 106}
107
108
109sub unimport {
110 shift;
e50aee73 111
112 my %names;
aeb5d71d 113 foreach (@_) {
d5201bd2 114 local $_ = _nativize($_);
115
116 my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir)
117 = _get_dirs($_);
4633a7c4 118 ++$names{$_};
d5201bd2 119 ++$names{$arch_dir} if -d $arch_auto_dir;
120 ++$names{$version_dir} if -d $version_dir;
121 ++$names{$version_arch_dir} if -d $version_arch_dir;
4633a7c4 122 }
e50aee73 123
aeb5d71d 124 # Remove ALL instances of each named directory.
125 @INC = grep { !exists $names{$_} } @INC;
abef537a 126 return;
e50aee73 127}
128
d5201bd2 129sub _get_dirs {
130 my($dir) = @_;
131 my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir);
132
133 # we could use this for all platforms in the future, but leave it
134 # Mac-only for now, until there is more time for testing it.
135 if ($Is_MacOS) {
136 $arch_auto_dir = File::Spec->catdir( $_, $archname, 'auto' );
137 $arch_dir = File::Spec->catdir( $_, $archname, );
138 $version_dir = File::Spec->catdir( $_, $version );
139 $version_arch_dir = File::Spec->catdir( $_, $version, $archname );
140 } else {
141 $arch_auto_dir = "$_/$archname/auto";
142 $arch_dir = "$_/$archname";
143 $version_dir = "$_/$version";
144 $version_arch_dir = "$_/$version/$archname";
145 }
146 return($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir);
147}
148
149sub _nativize {
150 my($dir) = @_;
151
5b865721 152 if ($Is_MacOS && $Mac_FS && ! -d $dir) {
d5201bd2 153 $dir = Mac::FileSpec::Unixish::nativize($dir);
154 $dir .= ":" unless $dir =~ /:$/;
155 }
156
157 return $dir;
158}
159
4633a7c4 1601;
e50aee73 161__END__
162
163=head1 NAME
164
165lib - manipulate @INC at compile time
166
167=head1 SYNOPSIS
168
169 use lib LIST;
170
171 no lib LIST;
172
173=head1 DESCRIPTION
174
175This is a small simple module which simplifies the manipulation of @INC
176at compile time.
177
178It is typically used to add extra directories to perl's search path so
179that later C<use> or C<require> statements will find modules which are
180not located on perl's default search path.
181
aeb5d71d 182=head2 Adding directories to @INC
e50aee73 183
184The parameters to C<use lib> are added to the start of the perl search
185path. Saying
186
187 use lib LIST;
188
4633a7c4 189is I<almost> the same as saying
e50aee73 190
191 BEGIN { unshift(@INC, LIST) }
192
4633a7c4 193For each directory in LIST (called $dir here) the lib module also
194checks to see if a directory called $dir/$archname/auto exists.
195If so the $dir/$archname directory is assumed to be a corresponding
196architecture specific directory and is added to @INC in front of $dir.
197
aeb5d71d 198To avoid memory leaks, all trailing duplicate entries in @INC are
199removed.
4633a7c4 200
aeb5d71d 201=head2 Deleting directories from @INC
e50aee73 202
203You should normally only add directories to @INC. If you need to
204delete directories from @INC take care to only delete those which you
205added yourself or which you are certain are not needed by other modules
206in your script. Other modules may have added directories which they
207need for correct operation.
208
aeb5d71d 209The C<no lib> statement deletes all instances of each named directory
210from @INC.
e50aee73 211
4633a7c4 212For each directory in LIST (called $dir here) the lib module also
213checks to see if a directory called $dir/$archname/auto exists.
214If so the $dir/$archname directory is assumed to be a corresponding
215architecture specific directory and is also deleted from @INC.
216
aeb5d71d 217=head2 Restoring original @INC
e50aee73 218
219When the lib module is first loaded it records the current value of @INC
220in an array C<@lib::ORIG_INC>. To restore @INC to that value you
4633a7c4 221can say
e50aee73 222
223 @INC = @lib::ORIG_INC;
224
e7bf5e49 225=head1 CAVEATS
226
227In order to keep lib.pm small and simple, it only works with Unix
228filepaths. This doesn't mean it only works on Unix, but non-Unix
229users must first translate their file paths to Unix conventions.
230
231 # VMS users wanting to put [.stuff.moo] into
232 # their @INC would write
233 use lib 'stuff/moo';
e50aee73 234
d5201bd2 235=head1 NOTES
236
237In the future, this module will likely use File::Spec for determining
238paths, as it does now for Mac OS (where Unix-style or Mac-style paths
239work, and Unix-style paths are converted properly to Mac-style paths
240before being added to @INC).
241
e50aee73 242=head1 SEE ALSO
243
af3dad46 244FindBin - optional module which deals with paths relative to the source file.
e50aee73 245
246=head1 AUTHOR
247
248Tim Bunce, 2nd June 1995.
249
250=cut
60ed1d8c 251!NO!SUBS!
252
253close OUT or die "Can't close $file: $!";
254chdir $origdir;