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