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