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