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