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