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