The thread begun by
[p5sagit/p5-mst-13.2.git] / lib / lib.pm.PL
CommitLineData
5ff5635a 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');
9
10my $Config_archname = defined($Config{'archname'}) ? $Config{'archname'} : '';
11my $Config_ver = defined($Config{'version'}) ? $Config{'version'} : '';
12my @Config_inc_version_list = defined($Config{'inc_version_list'}) ?
13 reverse split / /, $Config{'inc_version_list'} : ();
14
15open OUT,">$file" or die "Can't create $file: $!";
16
17print "Extracting $file (with variable substitutions)\n";
18
19# In this section, perl variables will be expanded during extraction.
20# You can use $Config{...} to use Configure variables.
21
22print OUT <<"!GROK!THIS!";
e50aee73 23package lib;
24
17f410f9 25use 5.005_64;
4633a7c4 26
5ff5635a 27my \$archname = "$Config_archname";
28my \$ver = "$Config_ver";
29my \@inc_version_list = qw(@Config_inc_version_list);
30
31!GROK!THIS!
32print OUT <<'!NO!SUBS!';
4633a7c4 33
17f410f9 34our @ORIG_INC = @INC; # take a handy copy of 'original' value
35our $VERSION = '0.5564';
e50aee73 36
37sub import {
38 shift;
aeb5d71d 39
40 my %names;
a5f75d66 41 foreach (reverse @_) {
016609bc 42 if ($_ eq '') {
af3dad46 43 require Carp;
774d564b 44 Carp::carp("Empty compile time value given to use lib");
af3dad46 45 }
20408e3c 46 if (-e && ! -d _) {
47 require Carp;
48 Carp::carp("Parameter to use lib must be directory, not file");
49 }
4633a7c4 50 unshift(@INC, $_);
29d82f8d 51 # Add any previous version directories we found at configure time
52 foreach my $incver (@inc_version_list)
53 {
54 unshift(@INC, "$_/$incver") if -d "$_/$incver";
55 }
4633a7c4 56 # Put a corresponding archlib directory infront of $_ if it
57 # looks like $_ has an archlib directory below it.
29d82f8d 58 unshift(@INC, "$_/$ver") if -d "$_/$ver";
59 unshift(@INC, "$_/$ver/$archname") if -d "$_/$ver/$archname";
4633a7c4 60 }
abef537a 61
62 # remove trailing duplicates
63 @INC = grep { ++$names{$_} == 1 } @INC;
64 return;
e50aee73 65}
66
67
68sub unimport {
69 shift;
e50aee73 70
71 my %names;
aeb5d71d 72 foreach (@_) {
4633a7c4 73 ++$names{$_};
74 ++$names{"$_/$archname"} if -d "$_/$archname/auto";
75 }
e50aee73 76
aeb5d71d 77 # Remove ALL instances of each named directory.
78 @INC = grep { !exists $names{$_} } @INC;
abef537a 79 return;
e50aee73 80}
81
4633a7c4 821;
e50aee73 83__END__
84
85=head1 NAME
86
87lib - manipulate @INC at compile time
88
89=head1 SYNOPSIS
90
91 use lib LIST;
92
93 no lib LIST;
94
95=head1 DESCRIPTION
96
97This is a small simple module which simplifies the manipulation of @INC
98at compile time.
99
100It is typically used to add extra directories to perl's search path so
101that later C<use> or C<require> statements will find modules which are
102not located on perl's default search path.
103
aeb5d71d 104=head2 Adding directories to @INC
e50aee73 105
106The parameters to C<use lib> are added to the start of the perl search
107path. Saying
108
109 use lib LIST;
110
4633a7c4 111is I<almost> the same as saying
e50aee73 112
113 BEGIN { unshift(@INC, LIST) }
114
4633a7c4 115For each directory in LIST (called $dir here) the lib module also
116checks to see if a directory called $dir/$archname/auto exists.
117If so the $dir/$archname directory is assumed to be a corresponding
118architecture specific directory and is added to @INC in front of $dir.
119
aeb5d71d 120To avoid memory leaks, all trailing duplicate entries in @INC are
121removed.
4633a7c4 122
aeb5d71d 123=head2 Deleting directories from @INC
e50aee73 124
125You should normally only add directories to @INC. If you need to
126delete directories from @INC take care to only delete those which you
127added yourself or which you are certain are not needed by other modules
128in your script. Other modules may have added directories which they
129need for correct operation.
130
aeb5d71d 131The C<no lib> statement deletes all instances of each named directory
132from @INC.
e50aee73 133
4633a7c4 134For each directory in LIST (called $dir here) the lib module also
135checks to see if a directory called $dir/$archname/auto exists.
136If so the $dir/$archname directory is assumed to be a corresponding
137architecture specific directory and is also deleted from @INC.
138
aeb5d71d 139=head2 Restoring original @INC
e50aee73 140
141When the lib module is first loaded it records the current value of @INC
142in an array C<@lib::ORIG_INC>. To restore @INC to that value you
4633a7c4 143can say
e50aee73 144
145 @INC = @lib::ORIG_INC;
146
e50aee73 147
148=head1 SEE ALSO
149
af3dad46 150FindBin - optional module which deals with paths relative to the source file.
e50aee73 151
152=head1 AUTHOR
153
154Tim Bunce, 2nd June 1995.
155
156=cut
5ff5635a 157!NO!SUBS!
158
159close OUT or die "Can't close $file: $!";
160chdir $origdir;