This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update to MakeMaker 5.34
[perl5.git] / lib / lib.pm
CommitLineData
e50aee73
AD
1package lib;
2
4633a7c4
LW
3use Config;
4
5my $archname = $Config{'archname'};
6
e50aee73
AD
7@ORIG_INC = (); # (avoid typo warning)
8@ORIG_INC = @INC; # take a handy copy of 'original' value
9
10
11sub import {
12 shift;
a5f75d66 13 foreach (reverse @_) {
4633a7c4
LW
14 unshift(@INC, $_);
15 # Put a corresponding archlib directory infront of $_ if it
16 # looks like $_ has an archlib directory below it.
17 unshift(@INC, "$_/$archname") if -d "$_/$archname/auto";
18 }
e50aee73
AD
19}
20
21
22sub unimport {
23 shift;
24 my $mode = shift if $_[0] =~ m/^:[A-Z]+/;
25
26 my %names;
4633a7c4
LW
27 foreach(@_) {
28 ++$names{$_};
29 ++$names{"$_/$archname"} if -d "$_/$archname/auto";
30 }
e50aee73
AD
31
32 if ($mode and $mode eq ':ALL') {
33 # Remove ALL instances of each named directory.
34 @INC = grep { !exists $names{$_} } @INC;
35 } else {
36 # Remove INITIAL instance(s) of each named directory.
37 @INC = grep { --$names{$_} < 0 } @INC;
38 }
39}
40
4633a7c4 411;
e50aee73
AD
42__END__
43
44=head1 NAME
45
46lib - manipulate @INC at compile time
47
48=head1 SYNOPSIS
49
50 use lib LIST;
51
52 no lib LIST;
53
54=head1 DESCRIPTION
55
56This is a small simple module which simplifies the manipulation of @INC
57at compile time.
58
59It is typically used to add extra directories to perl's search path so
60that later C<use> or C<require> statements will find modules which are
61not located on perl's default search path.
62
63
64=head2 ADDING DIRECTORIES TO @INC
65
66The parameters to C<use lib> are added to the start of the perl search
67path. Saying
68
69 use lib LIST;
70
4633a7c4 71is I<almost> the same as saying
e50aee73
AD
72
73 BEGIN { unshift(@INC, LIST) }
74
4633a7c4
LW
75For each directory in LIST (called $dir here) the lib module also
76checks to see if a directory called $dir/$archname/auto exists.
77If so the $dir/$archname directory is assumed to be a corresponding
78architecture specific directory and is added to @INC in front of $dir.
79
80If LIST includes both $dir and $dir/$archname then $dir/$archname will
81be added to @INC twice (if $dir/$archname/auto exists).
82
e50aee73
AD
83
84=head2 DELETING DIRECTORIES FROM @INC
85
86You should normally only add directories to @INC. If you need to
87delete directories from @INC take care to only delete those which you
88added yourself or which you are certain are not needed by other modules
89in your script. Other modules may have added directories which they
90need for correct operation.
91
92By default the C<no lib> statement deletes the I<first> instance of
93each named directory from @INC. To delete multiple instances of the
94same name from @INC you can specify the name multiple times.
95
96To delete I<all> instances of I<all> the specified names from @INC you can
97specify ':ALL' as the first parameter of C<no lib>. For example:
98
99 no lib qw(:ALL .);
100
4633a7c4
LW
101For each directory in LIST (called $dir here) the lib module also
102checks to see if a directory called $dir/$archname/auto exists.
103If so the $dir/$archname directory is assumed to be a corresponding
104architecture specific directory and is also deleted from @INC.
105
106If LIST includes both $dir and $dir/$archname then $dir/$archname will
107be deleted from @INC twice (if $dir/$archname/auto exists).
108
e50aee73
AD
109
110=head2 RESTORING ORIGINAL @INC
111
112When the lib module is first loaded it records the current value of @INC
113in an array C<@lib::ORIG_INC>. To restore @INC to that value you
4633a7c4 114can say
e50aee73
AD
115
116 @INC = @lib::ORIG_INC;
117
e50aee73
AD
118
119=head1 SEE ALSO
120
121AddINC - optional module which deals with paths relative to the source file.
122
123=head1 AUTHOR
124
125Tim Bunce, 2nd June 1995.
126
127=cut
128