avoid duplicates in @INC, they cause leaks in mod_perl etc
[p5sagit/p5-mst-13.2.git] / lib / lib.pm
CommitLineData
e50aee73 1package lib;
2
774d564b 3use vars qw(@ORIG_INC);
4633a7c4 4use Config;
5
6my $archname = $Config{'archname'};
7
e50aee73 8@ORIG_INC = @INC; # take a handy copy of 'original' value
9
10
11sub import {
12 shift;
aeb5d71d 13
14 my %names;
a5f75d66 15 foreach (reverse @_) {
016609bc 16 if ($_ eq '') {
af3dad46 17 require Carp;
774d564b 18 Carp::carp("Empty compile time value given to use lib");
af3dad46 19 }
20408e3c 20 if (-e && ! -d _) {
21 require Carp;
22 Carp::carp("Parameter to use lib must be directory, not file");
23 }
4633a7c4 24 unshift(@INC, $_);
25 # Put a corresponding archlib directory infront of $_ if it
26 # looks like $_ has an archlib directory below it.
774d564b 27 if (-d "$_/$archname") {
28 unshift(@INC, "$_/$archname") if -d "$_/$archname/auto";
29 unshift(@INC, "$_/$archname/$]") if -d "$_/$archname/$]/auto";
30 }
aeb5d71d 31 # remove trailing duplicates
32 @INC = grep { ++$names{$_} == 1 } @INC;
4633a7c4 33 }
e50aee73 34}
35
36
37sub unimport {
38 shift;
e50aee73 39
40 my %names;
aeb5d71d 41 foreach (@_) {
4633a7c4 42 ++$names{$_};
43 ++$names{"$_/$archname"} if -d "$_/$archname/auto";
44 }
e50aee73 45
aeb5d71d 46 # Remove ALL instances of each named directory.
47 @INC = grep { !exists $names{$_} } @INC;
e50aee73 48}
49
4633a7c4 501;
e50aee73 51__END__
52
53=head1 NAME
54
55lib - manipulate @INC at compile time
56
57=head1 SYNOPSIS
58
59 use lib LIST;
60
61 no lib LIST;
62
63=head1 DESCRIPTION
64
65This is a small simple module which simplifies the manipulation of @INC
66at compile time.
67
68It is typically used to add extra directories to perl's search path so
69that later C<use> or C<require> statements will find modules which are
70not located on perl's default search path.
71
aeb5d71d 72=head2 Adding directories to @INC
e50aee73 73
74The parameters to C<use lib> are added to the start of the perl search
75path. Saying
76
77 use lib LIST;
78
4633a7c4 79is I<almost> the same as saying
e50aee73 80
81 BEGIN { unshift(@INC, LIST) }
82
4633a7c4 83For each directory in LIST (called $dir here) the lib module also
84checks to see if a directory called $dir/$archname/auto exists.
85If so the $dir/$archname directory is assumed to be a corresponding
86architecture specific directory and is added to @INC in front of $dir.
87
aeb5d71d 88To avoid memory leaks, all trailing duplicate entries in @INC are
89removed.
4633a7c4 90
aeb5d71d 91=head2 Deleting directories from @INC
e50aee73 92
93You should normally only add directories to @INC. If you need to
94delete directories from @INC take care to only delete those which you
95added yourself or which you are certain are not needed by other modules
96in your script. Other modules may have added directories which they
97need for correct operation.
98
aeb5d71d 99The C<no lib> statement deletes all instances of each named directory
100from @INC.
e50aee73 101
4633a7c4 102For each directory in LIST (called $dir here) the lib module also
103checks to see if a directory called $dir/$archname/auto exists.
104If so the $dir/$archname directory is assumed to be a corresponding
105architecture specific directory and is also deleted from @INC.
106
aeb5d71d 107=head2 Restoring original @INC
e50aee73 108
109When the lib module is first loaded it records the current value of @INC
110in an array C<@lib::ORIG_INC>. To restore @INC to that value you
4633a7c4 111can say
e50aee73 112
113 @INC = @lib::ORIG_INC;
114
e50aee73 115
116=head1 SEE ALSO
117
af3dad46 118FindBin - optional module which deals with paths relative to the source file.
e50aee73 119
120=head1 AUTHOR
121
122Tim Bunce, 2nd June 1995.
123
124=cut