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