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'); | |
4755096e | 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 | ||
16 | # Expand the variables only if explicitly requested because | |
17 | # otherwise relocating Perl becomes much harder. | |
18 | ||
19 | if ($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 GS |
35 | |
36 | open OUT,">$file" or die "Can't create $file: $!"; | |
37 | ||
38 | print "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 | ||
43 | print OUT <<"!GROK!THIS!"; | |
e50aee73 AD |
44 | package lib; |
45 | ||
427f4adb TC |
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 GS |
49 | $useConfig |
50 | ||
51 | my \$archname = $Config_archname; | |
52 | my \$version = $Config_version; | |
53 | my \@inc_version_list = $Config_inc_version_list; | |
60ed1d8c GS |
54 | |
55 | !GROK!THIS! | |
56 | print OUT <<'!NO!SUBS!'; | |
4633a7c4 | 57 | |
17f410f9 GS |
58 | our @ORIG_INC = @INC; # take a handy copy of 'original' value |
59 | our $VERSION = '0.5564'; | |
e50aee73 AD |
60 | |
61 | sub import { | |
62 | shift; | |
aeb5d71d GS |
63 | |
64 | my %names; | |
a5f75d66 | 65 | foreach (reverse @_) { |
016609bc | 66 | if ($_ eq '') { |
af3dad46 | 67 | require Carp; |
774d564b | 68 | Carp::carp("Empty compile time value given to use lib"); |
af3dad46 | 69 | } |
20408e3c GS |
70 | if (-e && ! -d _) { |
71 | require Carp; | |
72 | Carp::carp("Parameter to use lib must be directory, not file"); | |
73 | } | |
4633a7c4 | 74 | unshift(@INC, $_); |
29d82f8d GS |
75 | # Add any previous version directories we found at configure time |
76 | foreach my $incver (@inc_version_list) | |
77 | { | |
78 | unshift(@INC, "$_/$incver") if -d "$_/$incver"; | |
79 | } | |
4633a7c4 LW |
80 | # Put a corresponding archlib directory infront of $_ if it |
81 | # looks like $_ has an archlib directory below it. | |
e9c6cca7 GS |
82 | unshift(@INC, "$_/$archname") if -d "$_/$archname/auto"; |
83 | unshift(@INC, "$_/$version") if -d "$_/$version"; | |
84 | unshift(@INC, "$_/$version/$archname") if -d "$_/$version/$archname"; | |
4633a7c4 | 85 | } |
abef537a GS |
86 | |
87 | # remove trailing duplicates | |
88 | @INC = grep { ++$names{$_} == 1 } @INC; | |
89 | return; | |
e50aee73 AD |
90 | } |
91 | ||
92 | ||
93 | sub unimport { | |
94 | shift; | |
e50aee73 AD |
95 | |
96 | my %names; | |
aeb5d71d | 97 | foreach (@_) { |
4633a7c4 | 98 | ++$names{$_}; |
e9c6cca7 GS |
99 | ++$names{"$_/$archname"} if -d "$_/$archname/auto"; |
100 | ++$names{"$_/$version"} if -d "$_/$version"; | |
101 | ++$names{"$_/$version/$archname"} if -d "$_/$version/$archname"; | |
4633a7c4 | 102 | } |
e50aee73 | 103 | |
aeb5d71d GS |
104 | # Remove ALL instances of each named directory. |
105 | @INC = grep { !exists $names{$_} } @INC; | |
abef537a | 106 | return; |
e50aee73 AD |
107 | } |
108 | ||
4633a7c4 | 109 | 1; |
e50aee73 AD |
110 | __END__ |
111 | ||
112 | =head1 NAME | |
113 | ||
114 | lib - manipulate @INC at compile time | |
115 | ||
116 | =head1 SYNOPSIS | |
117 | ||
118 | use lib LIST; | |
119 | ||
120 | no lib LIST; | |
121 | ||
122 | =head1 DESCRIPTION | |
123 | ||
124 | This is a small simple module which simplifies the manipulation of @INC | |
125 | at compile time. | |
126 | ||
127 | It is typically used to add extra directories to perl's search path so | |
128 | that later C<use> or C<require> statements will find modules which are | |
129 | not located on perl's default search path. | |
130 | ||
aeb5d71d | 131 | =head2 Adding directories to @INC |
e50aee73 AD |
132 | |
133 | The parameters to C<use lib> are added to the start of the perl search | |
134 | path. Saying | |
135 | ||
136 | use lib LIST; | |
137 | ||
4633a7c4 | 138 | is I<almost> the same as saying |
e50aee73 AD |
139 | |
140 | BEGIN { unshift(@INC, LIST) } | |
141 | ||
4633a7c4 LW |
142 | For each directory in LIST (called $dir here) the lib module also |
143 | checks to see if a directory called $dir/$archname/auto exists. | |
144 | If so the $dir/$archname directory is assumed to be a corresponding | |
145 | architecture specific directory and is added to @INC in front of $dir. | |
146 | ||
aeb5d71d GS |
147 | To avoid memory leaks, all trailing duplicate entries in @INC are |
148 | removed. | |
4633a7c4 | 149 | |
aeb5d71d | 150 | =head2 Deleting directories from @INC |
e50aee73 AD |
151 | |
152 | You should normally only add directories to @INC. If you need to | |
153 | delete directories from @INC take care to only delete those which you | |
154 | added yourself or which you are certain are not needed by other modules | |
155 | in your script. Other modules may have added directories which they | |
156 | need for correct operation. | |
157 | ||
aeb5d71d GS |
158 | The C<no lib> statement deletes all instances of each named directory |
159 | from @INC. | |
e50aee73 | 160 | |
4633a7c4 LW |
161 | For each directory in LIST (called $dir here) the lib module also |
162 | checks to see if a directory called $dir/$archname/auto exists. | |
163 | If so the $dir/$archname directory is assumed to be a corresponding | |
164 | architecture specific directory and is also deleted from @INC. | |
165 | ||
aeb5d71d | 166 | =head2 Restoring original @INC |
e50aee73 AD |
167 | |
168 | When the lib module is first loaded it records the current value of @INC | |
169 | in an array C<@lib::ORIG_INC>. To restore @INC to that value you | |
4633a7c4 | 170 | can say |
e50aee73 AD |
171 | |
172 | @INC = @lib::ORIG_INC; | |
173 | ||
e7bf5e49 MS |
174 | =head1 CAVEATS |
175 | ||
176 | In order to keep lib.pm small and simple, it only works with Unix | |
177 | filepaths. This doesn't mean it only works on Unix, but non-Unix | |
178 | users must first translate their file paths to Unix conventions. | |
179 | ||
180 | # VMS users wanting to put [.stuff.moo] into | |
181 | # their @INC would write | |
182 | use lib 'stuff/moo'; | |
e50aee73 AD |
183 | |
184 | =head1 SEE ALSO | |
185 | ||
af3dad46 | 186 | FindBin - optional module which deals with paths relative to the source file. |
e50aee73 AD |
187 | |
188 | =head1 AUTHOR | |
189 | ||
190 | Tim Bunce, 2nd June 1995. | |
191 | ||
192 | =cut | |
60ed1d8c GS |
193 | !NO!SUBS! |
194 | ||
195 | close OUT or die "Can't close $file: $!"; | |
196 | chdir $origdir; |