This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [ID 20020422.003] Suggestion in Perl 5.6.1 installation on AIX
[perl5.git] / lib / lib_pm.PL
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');
9 $file =~ s!_(pm)$!.$1!i;
10
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 =
33               q(reverse split / /, $Config{inc_version_list});
34 }
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!";
44 package lib;
45
46 # THIS FILE IS AUTOMATICALLY GENERATED FROM lib_pm.PL.
47 # ANY CHANGES TO THIS FILE WILL BE OVERWRITTEN BY THE NEXT PERL BUILD.
48
49 $useConfig
50
51 my \$archname         = $Config_archname;
52 my \$version          = $Config_version;
53 my \@inc_version_list = $Config_inc_version_list;
54
55 !GROK!THIS!
56 print OUT <<'!NO!SUBS!';
57
58 our @ORIG_INC = @INC;   # take a handy copy of 'original' value
59 our $VERSION = '0.5564';
60
61 sub import {
62     shift;
63
64     my %names;
65     foreach (reverse @_) {
66         if ($_ eq '') {
67             require Carp;
68             Carp::carp("Empty compile time value given to use lib");
69         }
70         if (-e && ! -d _) {
71             require Carp;
72             Carp::carp("Parameter to use lib must be directory, not file");
73         }
74         unshift(@INC, $_);
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         }
80         # Put a corresponding archlib directory infront of $_ if it
81         # looks like $_ has an archlib directory below it.
82         unshift(@INC, "$_/$archname")          if -d "$_/$archname/auto";
83         unshift(@INC, "$_/$version")           if -d "$_/$version";
84         unshift(@INC, "$_/$version/$archname") if -d "$_/$version/$archname";
85     }
86
87     # remove trailing duplicates
88     @INC = grep { ++$names{$_} == 1 } @INC;
89     return;
90 }
91
92
93 sub unimport {
94     shift;
95
96     my %names;
97     foreach (@_) {
98         ++$names{$_};
99         ++$names{"$_/$archname"}          if -d "$_/$archname/auto";
100         ++$names{"$_/$version"}           if -d "$_/$version";
101         ++$names{"$_/$version/$archname"} if -d "$_/$version/$archname";
102     }
103
104     # Remove ALL instances of each named directory.
105     @INC = grep { !exists $names{$_} } @INC;
106     return;
107 }
108
109 1;
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
131 =head2 Adding directories to @INC
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
138 is I<almost> the same as saying
139
140     BEGIN { unshift(@INC, LIST) }
141
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
147 To avoid memory leaks, all trailing duplicate entries in @INC are
148 removed.
149
150 =head2 Deleting directories from @INC
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
158 The C<no lib> statement deletes all instances of each named directory
159 from @INC.
160
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
166 =head2 Restoring original @INC
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
170 can say
171
172     @INC = @lib::ORIG_INC;
173
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';
183
184 =head1 SEE ALSO
185
186 FindBin - optional module which deals with paths relative to the source file.
187
188 =head1 AUTHOR
189
190 Tim Bunce, 2nd June 1995.
191
192 =cut
193 !NO!SUBS!
194
195 close OUT or die "Can't close $file: $!";
196 chdir $origdir;