xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/lib_pm.PL (revision 0:68f95e015346)
1*0Sstevel@tonic-gateuse Config;
2*0Sstevel@tonic-gateuse File::Basename qw(&basename &dirname);
3*0Sstevel@tonic-gateuse File::Spec;
4*0Sstevel@tonic-gateuse Cwd;
5*0Sstevel@tonic-gate
6*0Sstevel@tonic-gatemy $origdir = cwd;
7*0Sstevel@tonic-gatechdir dirname($0);
8*0Sstevel@tonic-gatemy $file = basename($0, '.PL');
9*0Sstevel@tonic-gate$file =~ s!_(pm)$!.$1!i;
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gatemy $useConfig;
12*0Sstevel@tonic-gatemy $Config_archname;
13*0Sstevel@tonic-gatemy $Config_version;
14*0Sstevel@tonic-gatemy $Config_inc_version_list;
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gate# Expand the variables only if explicitly requested because
17*0Sstevel@tonic-gate# otherwise relocating Perl becomes much harder.
18*0Sstevel@tonic-gate
19*0Sstevel@tonic-gateif ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) {
20*0Sstevel@tonic-gate    $useConfig = '';
21*0Sstevel@tonic-gate    $Config_archname = qq('$Config{archname}');
22*0Sstevel@tonic-gate    $Config_version  = qq('$Config{version}');
23*0Sstevel@tonic-gate    my @Config_inc_version_list =
24*0Sstevel@tonic-gate	reverse split / /, $Config{inc_version_list};
25*0Sstevel@tonic-gate    $Config_inc_version_list =
26*0Sstevel@tonic-gate	@Config_inc_version_list ?
27*0Sstevel@tonic-gate	    qq(@Config_inc_version_list) : q(());
28*0Sstevel@tonic-gate} else {
29*0Sstevel@tonic-gate    $useConfig = 'use Config;';
30*0Sstevel@tonic-gate    $Config_archname = q($Config{archname});
31*0Sstevel@tonic-gate    $Config_version  = q($Config{version});
32*0Sstevel@tonic-gate    $Config_inc_version_list =
33*0Sstevel@tonic-gate	      q(reverse split / /, $Config{inc_version_list});
34*0Sstevel@tonic-gate}
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gateopen OUT,">$file" or die "Can't create $file: $!";
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gateprint "Extracting $file (with variable substitutions)\n";
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate# In this section, perl variables will be expanded during extraction.
41*0Sstevel@tonic-gate# You can use $Config{...} to use Configure variables.
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gateprint OUT <<"!GROK!THIS!";
44*0Sstevel@tonic-gatepackage lib;
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate# THIS FILE IS AUTOMATICALLY GENERATED FROM lib_pm.PL.
47*0Sstevel@tonic-gate# ANY CHANGES TO THIS FILE WILL BE OVERWRITTEN BY THE NEXT PERL BUILD.
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate$useConfig
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gateuse strict;
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gatemy \$archname         = $Config_archname;
54*0Sstevel@tonic-gatemy \$version          = $Config_version;
55*0Sstevel@tonic-gatemy \@inc_version_list = $Config_inc_version_list;
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate!GROK!THIS!
58*0Sstevel@tonic-gateprint OUT <<'!NO!SUBS!';
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gateour @ORIG_INC = @INC;	# take a handy copy of 'original' value
61*0Sstevel@tonic-gateour $VERSION = '0.5565';
62*0Sstevel@tonic-gatemy $Is_MacOS = $^O eq 'MacOS';
63*0Sstevel@tonic-gatemy $Mac_FS;
64*0Sstevel@tonic-gateif ($Is_MacOS) {
65*0Sstevel@tonic-gate	require File::Spec;
66*0Sstevel@tonic-gate	$Mac_FS = eval { require Mac::FileSpec::Unixish };
67*0Sstevel@tonic-gate}
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gatesub import {
70*0Sstevel@tonic-gate    shift;
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate    my %names;
73*0Sstevel@tonic-gate    foreach (reverse @_) {
74*0Sstevel@tonic-gate	my $path = $_;		# we'll be modifying it, so break the alias
75*0Sstevel@tonic-gate	if ($path eq '') {
76*0Sstevel@tonic-gate	    require Carp;
77*0Sstevel@tonic-gate	    Carp::carp("Empty compile time value given to use lib");
78*0Sstevel@tonic-gate	}
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate	$path = _nativize($path);
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate	if (-e $path && ! -d _) {
83*0Sstevel@tonic-gate	    require Carp;
84*0Sstevel@tonic-gate	    Carp::carp("Parameter to use lib must be directory, not file");
85*0Sstevel@tonic-gate	}
86*0Sstevel@tonic-gate	unshift(@INC, $path);
87*0Sstevel@tonic-gate	# Add any previous version directories we found at configure time
88*0Sstevel@tonic-gate	foreach my $incver (@inc_version_list)
89*0Sstevel@tonic-gate	{
90*0Sstevel@tonic-gate	    my $dir = $Is_MacOS
91*0Sstevel@tonic-gate		? File::Spec->catdir( $path, $incver )
92*0Sstevel@tonic-gate		: "$path/$incver";
93*0Sstevel@tonic-gate	    unshift(@INC, $dir) if -d $dir;
94*0Sstevel@tonic-gate	}
95*0Sstevel@tonic-gate	# Put a corresponding archlib directory in front of $path if it
96*0Sstevel@tonic-gate	# looks like $path has an archlib directory below it.
97*0Sstevel@tonic-gate	my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir)
98*0Sstevel@tonic-gate	    = _get_dirs($path);
99*0Sstevel@tonic-gate	unshift(@INC, $arch_dir)         if -d $arch_auto_dir;
100*0Sstevel@tonic-gate	unshift(@INC, $version_dir)      if -d $version_dir;
101*0Sstevel@tonic-gate	unshift(@INC, $version_arch_dir) if -d $version_arch_dir;
102*0Sstevel@tonic-gate    }
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate    # remove trailing duplicates
105*0Sstevel@tonic-gate    @INC = grep { ++$names{$_} == 1 } @INC;
106*0Sstevel@tonic-gate    return;
107*0Sstevel@tonic-gate}
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gatesub unimport {
111*0Sstevel@tonic-gate    shift;
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate    my %names;
114*0Sstevel@tonic-gate    foreach (@_) {
115*0Sstevel@tonic-gate	my $path = _nativize($_);
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate	my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir)
118*0Sstevel@tonic-gate	    = _get_dirs($path);
119*0Sstevel@tonic-gate	++$names{$path};
120*0Sstevel@tonic-gate	++$names{$arch_dir}         if -d $arch_auto_dir;
121*0Sstevel@tonic-gate	++$names{$version_dir}      if -d $version_dir;
122*0Sstevel@tonic-gate	++$names{$version_arch_dir} if -d $version_arch_dir;
123*0Sstevel@tonic-gate    }
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate    # Remove ALL instances of each named directory.
126*0Sstevel@tonic-gate    @INC = grep { !exists $names{$_} } @INC;
127*0Sstevel@tonic-gate    return;
128*0Sstevel@tonic-gate}
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gatesub _get_dirs {
131*0Sstevel@tonic-gate    my($dir) = @_;
132*0Sstevel@tonic-gate    my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir);
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate    # we could use this for all platforms in the future, but leave it
135*0Sstevel@tonic-gate    # Mac-only for now, until there is more time for testing it.
136*0Sstevel@tonic-gate    if ($Is_MacOS) {
137*0Sstevel@tonic-gate	$arch_auto_dir    = File::Spec->catdir( $dir, $archname, 'auto' );
138*0Sstevel@tonic-gate	$arch_dir         = File::Spec->catdir( $dir, $archname, );
139*0Sstevel@tonic-gate	$version_dir      = File::Spec->catdir( $dir, $version );
140*0Sstevel@tonic-gate	$version_arch_dir = File::Spec->catdir( $dir, $version, $archname );
141*0Sstevel@tonic-gate    } else {
142*0Sstevel@tonic-gate	$arch_auto_dir    = "$dir/$archname/auto";
143*0Sstevel@tonic-gate	$arch_dir         = "$dir/$archname";
144*0Sstevel@tonic-gate	$version_dir      = "$dir/$version";
145*0Sstevel@tonic-gate	$version_arch_dir = "$dir/$version/$archname";
146*0Sstevel@tonic-gate    }
147*0Sstevel@tonic-gate    return($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir);
148*0Sstevel@tonic-gate}
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gatesub _nativize {
151*0Sstevel@tonic-gate    my($dir) = @_;
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate    if ($Is_MacOS && $Mac_FS && ! -d $dir) {
154*0Sstevel@tonic-gate	$dir = Mac::FileSpec::Unixish::nativize($dir);
155*0Sstevel@tonic-gate	$dir .= ":" unless $dir =~ /:$/;
156*0Sstevel@tonic-gate    }
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate    return $dir;
159*0Sstevel@tonic-gate}
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate1;
162*0Sstevel@tonic-gate__END__
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate=head1 NAME
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gatelib - manipulate @INC at compile time
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate=head1 SYNOPSIS
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate    use lib LIST;
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gate    no lib LIST;
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate=head1 DESCRIPTION
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gateThis is a small simple module which simplifies the manipulation of @INC
177*0Sstevel@tonic-gateat compile time.
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gateIt is typically used to add extra directories to perl's search path so
180*0Sstevel@tonic-gatethat later C<use> or C<require> statements will find modules which are
181*0Sstevel@tonic-gatenot located on perl's default search path.
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gate=head2 Adding directories to @INC
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gateThe parameters to C<use lib> are added to the start of the perl search
186*0Sstevel@tonic-gatepath. Saying
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate    use lib LIST;
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gateis I<almost> the same as saying
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate    BEGIN { unshift(@INC, LIST) }
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gateFor each directory in LIST (called $dir here) the lib module also
195*0Sstevel@tonic-gatechecks to see if a directory called $dir/$archname/auto exists.
196*0Sstevel@tonic-gateIf so the $dir/$archname directory is assumed to be a corresponding
197*0Sstevel@tonic-gatearchitecture specific directory and is added to @INC in front of $dir.
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gateTo avoid memory leaks, all trailing duplicate entries in @INC are
200*0Sstevel@tonic-gateremoved.
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate=head2 Deleting directories from @INC
203*0Sstevel@tonic-gate
204*0Sstevel@tonic-gateYou should normally only add directories to @INC.  If you need to
205*0Sstevel@tonic-gatedelete directories from @INC take care to only delete those which you
206*0Sstevel@tonic-gateadded yourself or which you are certain are not needed by other modules
207*0Sstevel@tonic-gatein your script.  Other modules may have added directories which they
208*0Sstevel@tonic-gateneed for correct operation.
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gateThe C<no lib> statement deletes all instances of each named directory
211*0Sstevel@tonic-gatefrom @INC.
212*0Sstevel@tonic-gate
213*0Sstevel@tonic-gateFor each directory in LIST (called $dir here) the lib module also
214*0Sstevel@tonic-gatechecks to see if a directory called $dir/$archname/auto exists.
215*0Sstevel@tonic-gateIf so the $dir/$archname directory is assumed to be a corresponding
216*0Sstevel@tonic-gatearchitecture specific directory and is also deleted from @INC.
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate=head2 Restoring original @INC
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gateWhen the lib module is first loaded it records the current value of @INC
221*0Sstevel@tonic-gatein an array C<@lib::ORIG_INC>. To restore @INC to that value you
222*0Sstevel@tonic-gatecan say
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gate    @INC = @lib::ORIG_INC;
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate=head1 CAVEATS
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gateIn order to keep lib.pm small and simple, it only works with Unix
229*0Sstevel@tonic-gatefilepaths.  This doesn't mean it only works on Unix, but non-Unix
230*0Sstevel@tonic-gateusers must first translate their file paths to Unix conventions.
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate    # VMS users wanting to put [.stuff.moo] into
233*0Sstevel@tonic-gate    # their @INC would write
234*0Sstevel@tonic-gate    use lib 'stuff/moo';
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate=head1 NOTES
237*0Sstevel@tonic-gate
238*0Sstevel@tonic-gateIn the future, this module will likely use File::Spec for determining
239*0Sstevel@tonic-gatepaths, as it does now for Mac OS (where Unix-style or Mac-style paths
240*0Sstevel@tonic-gatework, and Unix-style paths are converted properly to Mac-style paths
241*0Sstevel@tonic-gatebefore being added to @INC).
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gate=head1 SEE ALSO
244*0Sstevel@tonic-gate
245*0Sstevel@tonic-gateFindBin - optional module which deals with paths relative to the source file.
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate=head1 AUTHOR
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gateTim Bunce, 2nd June 1995.
250*0Sstevel@tonic-gate
251*0Sstevel@tonic-gate=cut
252*0Sstevel@tonic-gate!NO!SUBS!
253*0Sstevel@tonic-gate
254*0Sstevel@tonic-gateclose OUT or die "Can't close $file: $!";
255*0Sstevel@tonic-gatechdir $origdir;
256