Commit | Line | Data |
e50aee73 |
1 | package lib; |
2 | |
17f410f9 |
3 | use 5.005_64; |
4633a7c4 |
4 | use Config; |
5 | |
6 | my $archname = $Config{'archname'}; |
0ff3fa1a |
7 | my $ver = $Config{'version'}; |
29d82f8d |
8 | my @inc_version_list = reverse split / /, $Config{'inc_version_list'}; |
4633a7c4 |
9 | |
17f410f9 |
10 | our @ORIG_INC = @INC; # take a handy copy of 'original' value |
11 | our $VERSION = '0.5564'; |
e50aee73 |
12 | |
13 | sub import { |
14 | shift; |
aeb5d71d |
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, $_); |
29d82f8d |
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. |
29d82f8d |
34 | unshift(@INC, "$_/$ver") if -d "$_/$ver"; |
35 | unshift(@INC, "$_/$ver/$archname") if -d "$_/$ver/$archname"; |
4633a7c4 |
36 | } |
abef537a |
37 | |
38 | # remove trailing duplicates |
39 | @INC = grep { ++$names{$_} == 1 } @INC; |
40 | return; |
e50aee73 |
41 | } |
42 | |
43 | |
44 | sub unimport { |
45 | shift; |
e50aee73 |
46 | |
47 | my %names; |
aeb5d71d |
48 | foreach (@_) { |
4633a7c4 |
49 | ++$names{$_}; |
50 | ++$names{"$_/$archname"} if -d "$_/$archname/auto"; |
51 | } |
e50aee73 |
52 | |
aeb5d71d |
53 | # Remove ALL instances of each named directory. |
54 | @INC = grep { !exists $names{$_} } @INC; |
abef537a |
55 | return; |
e50aee73 |
56 | } |
57 | |
4633a7c4 |
58 | 1; |
e50aee73 |
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 | |
aeb5d71d |
80 | =head2 Adding directories to @INC |
e50aee73 |
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 | |
4633a7c4 |
87 | is I<almost> the same as saying |
e50aee73 |
88 | |
89 | BEGIN { unshift(@INC, LIST) } |
90 | |
4633a7c4 |
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 | |
aeb5d71d |
96 | To avoid memory leaks, all trailing duplicate entries in @INC are |
97 | removed. |
4633a7c4 |
98 | |
aeb5d71d |
99 | =head2 Deleting directories from @INC |
e50aee73 |
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 | |
aeb5d71d |
107 | The C<no lib> statement deletes all instances of each named directory |
108 | from @INC. |
e50aee73 |
109 | |
4633a7c4 |
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 | |
aeb5d71d |
115 | =head2 Restoring original @INC |
e50aee73 |
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 |
4633a7c4 |
119 | can say |
e50aee73 |
120 | |
121 | @INC = @lib::ORIG_INC; |
122 | |
e50aee73 |
123 | |
124 | =head1 SEE ALSO |
125 | |
af3dad46 |
126 | FindBin - optional module which deals with paths relative to the source file. |
e50aee73 |
127 | |
128 | =head1 AUTHOR |
129 | |
130 | Tim Bunce, 2nd June 1995. |
131 | |
132 | =cut |