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