Commit | Line | Data |
5ff5635a |
1 | use Config; |
2 | use File::Basename qw(&basename &dirname); |
3 | use File::Spec; |
4 | use Cwd; |
5 | |
6 | my $origdir = cwd; |
7 | chdir dirname($0); |
8 | my $file = basename($0, '.PL'); |
9 | |
10 | my $Config_archname = defined($Config{'archname'}) ? $Config{'archname'} : ''; |
11 | my $Config_ver = defined($Config{'version'}) ? $Config{'version'} : ''; |
12 | my @Config_inc_version_list = defined($Config{'inc_version_list'}) ? |
13 | reverse split / /, $Config{'inc_version_list'} : (); |
14 | |
15 | open OUT,">$file" or die "Can't create $file: $!"; |
16 | |
17 | print "Extracting $file (with variable substitutions)\n"; |
18 | |
19 | # In this section, perl variables will be expanded during extraction. |
20 | # You can use $Config{...} to use Configure variables. |
21 | |
22 | print OUT <<"!GROK!THIS!"; |
e50aee73 |
23 | package lib; |
24 | |
17f410f9 |
25 | use 5.005_64; |
4633a7c4 |
26 | |
5ff5635a |
27 | my \$archname = "$Config_archname"; |
28 | my \$ver = "$Config_ver"; |
29 | my \@inc_version_list = qw(@Config_inc_version_list); |
30 | |
31 | !GROK!THIS! |
32 | print OUT <<'!NO!SUBS!'; |
4633a7c4 |
33 | |
17f410f9 |
34 | our @ORIG_INC = @INC; # take a handy copy of 'original' value |
35 | our $VERSION = '0.5564'; |
e50aee73 |
36 | |
37 | sub import { |
38 | shift; |
aeb5d71d |
39 | |
40 | my %names; |
a5f75d66 |
41 | foreach (reverse @_) { |
016609bc |
42 | if ($_ eq '') { |
af3dad46 |
43 | require Carp; |
774d564b |
44 | Carp::carp("Empty compile time value given to use lib"); |
af3dad46 |
45 | } |
20408e3c |
46 | if (-e && ! -d _) { |
47 | require Carp; |
48 | Carp::carp("Parameter to use lib must be directory, not file"); |
49 | } |
4633a7c4 |
50 | unshift(@INC, $_); |
29d82f8d |
51 | # Add any previous version directories we found at configure time |
52 | foreach my $incver (@inc_version_list) |
53 | { |
54 | unshift(@INC, "$_/$incver") if -d "$_/$incver"; |
55 | } |
4633a7c4 |
56 | # Put a corresponding archlib directory infront of $_ if it |
57 | # looks like $_ has an archlib directory below it. |
29d82f8d |
58 | unshift(@INC, "$_/$ver") if -d "$_/$ver"; |
59 | unshift(@INC, "$_/$ver/$archname") if -d "$_/$ver/$archname"; |
4633a7c4 |
60 | } |
abef537a |
61 | |
62 | # remove trailing duplicates |
63 | @INC = grep { ++$names{$_} == 1 } @INC; |
64 | return; |
e50aee73 |
65 | } |
66 | |
67 | |
68 | sub unimport { |
69 | shift; |
e50aee73 |
70 | |
71 | my %names; |
aeb5d71d |
72 | foreach (@_) { |
4633a7c4 |
73 | ++$names{$_}; |
74 | ++$names{"$_/$archname"} if -d "$_/$archname/auto"; |
75 | } |
e50aee73 |
76 | |
aeb5d71d |
77 | # Remove ALL instances of each named directory. |
78 | @INC = grep { !exists $names{$_} } @INC; |
abef537a |
79 | return; |
e50aee73 |
80 | } |
81 | |
4633a7c4 |
82 | 1; |
e50aee73 |
83 | __END__ |
84 | |
85 | =head1 NAME |
86 | |
87 | lib - manipulate @INC at compile time |
88 | |
89 | =head1 SYNOPSIS |
90 | |
91 | use lib LIST; |
92 | |
93 | no lib LIST; |
94 | |
95 | =head1 DESCRIPTION |
96 | |
97 | This is a small simple module which simplifies the manipulation of @INC |
98 | at compile time. |
99 | |
100 | It is typically used to add extra directories to perl's search path so |
101 | that later C<use> or C<require> statements will find modules which are |
102 | not located on perl's default search path. |
103 | |
aeb5d71d |
104 | =head2 Adding directories to @INC |
e50aee73 |
105 | |
106 | The parameters to C<use lib> are added to the start of the perl search |
107 | path. Saying |
108 | |
109 | use lib LIST; |
110 | |
4633a7c4 |
111 | is I<almost> the same as saying |
e50aee73 |
112 | |
113 | BEGIN { unshift(@INC, LIST) } |
114 | |
4633a7c4 |
115 | For each directory in LIST (called $dir here) the lib module also |
116 | checks to see if a directory called $dir/$archname/auto exists. |
117 | If so the $dir/$archname directory is assumed to be a corresponding |
118 | architecture specific directory and is added to @INC in front of $dir. |
119 | |
aeb5d71d |
120 | To avoid memory leaks, all trailing duplicate entries in @INC are |
121 | removed. |
4633a7c4 |
122 | |
aeb5d71d |
123 | =head2 Deleting directories from @INC |
e50aee73 |
124 | |
125 | You should normally only add directories to @INC. If you need to |
126 | delete directories from @INC take care to only delete those which you |
127 | added yourself or which you are certain are not needed by other modules |
128 | in your script. Other modules may have added directories which they |
129 | need for correct operation. |
130 | |
aeb5d71d |
131 | The C<no lib> statement deletes all instances of each named directory |
132 | from @INC. |
e50aee73 |
133 | |
4633a7c4 |
134 | For each directory in LIST (called $dir here) the lib module also |
135 | checks to see if a directory called $dir/$archname/auto exists. |
136 | If so the $dir/$archname directory is assumed to be a corresponding |
137 | architecture specific directory and is also deleted from @INC. |
138 | |
aeb5d71d |
139 | =head2 Restoring original @INC |
e50aee73 |
140 | |
141 | When the lib module is first loaded it records the current value of @INC |
142 | in an array C<@lib::ORIG_INC>. To restore @INC to that value you |
4633a7c4 |
143 | can say |
e50aee73 |
144 | |
145 | @INC = @lib::ORIG_INC; |
146 | |
e50aee73 |
147 | |
148 | =head1 SEE ALSO |
149 | |
af3dad46 |
150 | FindBin - optional module which deals with paths relative to the source file. |
e50aee73 |
151 | |
152 | =head1 AUTHOR |
153 | |
154 | Tim Bunce, 2nd June 1995. |
155 | |
156 | =cut |
5ff5635a |
157 | !NO!SUBS! |
158 | |
159 | close OUT or die "Can't close $file: $!"; |
160 | chdir $origdir; |