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