| 1 | package lib; |
| 2 | |
| 3 | # THIS FILE IS AUTOMATICALLY GENERATED FROM lib_pm.PL. |
| 4 | # ANY CHANGES TO THIS FILE WILL BE OVERWRITTEN BY THE NEXT PERL BUILD. |
| 5 | |
| 6 | use Config; |
| 7 | |
| 8 | use strict; |
| 9 | |
| 10 | my $archname = $Config{archname}; |
| 11 | my $version = $Config{version}; |
| 12 | my @inc_version_list = reverse split / /, $Config{inc_version_list}; |
| 13 | |
| 14 | |
| 15 | our @ORIG_INC = @INC; # take a handy copy of 'original' value |
| 16 | our $VERSION = '0.5564'; |
| 17 | my $Is_MacOS = $^O eq 'MacOS'; |
| 18 | my $Mac_FS; |
| 19 | if ($Is_MacOS) { |
| 20 | require File::Spec; |
| 21 | $Mac_FS = eval { require Mac::FileSpec::Unixish }; |
| 22 | } |
| 23 | |
| 24 | sub import { |
| 25 | shift; |
| 26 | |
| 27 | my %names; |
| 28 | foreach (reverse @_) { |
| 29 | if ($_ eq '') { |
| 30 | require Carp; |
| 31 | Carp::carp("Empty compile time value given to use lib"); |
| 32 | } |
| 33 | |
| 34 | local $_ = _nativize($_); |
| 35 | |
| 36 | if (-e && ! -d _) { |
| 37 | require Carp; |
| 38 | Carp::carp("Parameter to use lib must be directory, not file"); |
| 39 | } |
| 40 | unshift(@INC, $_); |
| 41 | # Add any previous version directories we found at configure time |
| 42 | foreach my $incver (@inc_version_list) |
| 43 | { |
| 44 | my $dir = $Is_MacOS |
| 45 | ? File::Spec->catdir( $_, $incver ) |
| 46 | : "$_/$incver"; |
| 47 | unshift(@INC, $dir) if -d $dir; |
| 48 | } |
| 49 | # Put a corresponding archlib directory in front of $_ if it |
| 50 | # looks like $_ has an archlib directory below it. |
| 51 | my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir) |
| 52 | = _get_dirs($_); |
| 53 | unshift(@INC, $arch_dir) if -d $arch_auto_dir; |
| 54 | unshift(@INC, $version_dir) if -d $version_dir; |
| 55 | unshift(@INC, $version_arch_dir) if -d $version_arch_dir; |
| 56 | } |
| 57 | |
| 58 | # remove trailing duplicates |
| 59 | @INC = grep { ++$names{$_} == 1 } @INC; |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | sub unimport { |
| 65 | shift; |
| 66 | |
| 67 | my %names; |
| 68 | foreach (@_) { |
| 69 | local $_ = _nativize($_); |
| 70 | |
| 71 | my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir) |
| 72 | = _get_dirs($_); |
| 73 | ++$names{$_}; |
| 74 | ++$names{$arch_dir} if -d $arch_auto_dir; |
| 75 | ++$names{$version_dir} if -d $version_dir; |
| 76 | ++$names{$version_arch_dir} if -d $version_arch_dir; |
| 77 | } |
| 78 | |
| 79 | # Remove ALL instances of each named directory. |
| 80 | @INC = grep { !exists $names{$_} } @INC; |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | sub _get_dirs { |
| 85 | my($dir) = @_; |
| 86 | my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir); |
| 87 | |
| 88 | # we could use this for all platforms in the future, but leave it |
| 89 | # Mac-only for now, until there is more time for testing it. |
| 90 | if ($Is_MacOS) { |
| 91 | $arch_auto_dir = File::Spec->catdir( $_, $archname, 'auto' ); |
| 92 | $arch_dir = File::Spec->catdir( $_, $archname, ); |
| 93 | $version_dir = File::Spec->catdir( $_, $version ); |
| 94 | $version_arch_dir = File::Spec->catdir( $_, $version, $archname ); |
| 95 | } else { |
| 96 | $arch_auto_dir = "$_/$archname/auto"; |
| 97 | $arch_dir = "$_/$archname"; |
| 98 | $version_dir = "$_/$version"; |
| 99 | $version_arch_dir = "$_/$version/$archname"; |
| 100 | } |
| 101 | return($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir); |
| 102 | } |
| 103 | |
| 104 | sub _nativize { |
| 105 | my($dir) = @_; |
| 106 | |
| 107 | if ($Is_MacOS && $Mac_FS && ! -d $dir) { |
| 108 | $dir = Mac::FileSpec::Unixish::nativize($dir); |
| 109 | $dir .= ":" unless $dir =~ /:$/; |
| 110 | } |
| 111 | |
| 112 | return $dir; |
| 113 | } |
| 114 | |
| 115 | 1; |
| 116 | __END__ |
| 117 | |
| 118 | =head1 NAME |
| 119 | |
| 120 | lib - manipulate @INC at compile time |
| 121 | |
| 122 | =head1 SYNOPSIS |
| 123 | |
| 124 | use lib LIST; |
| 125 | |
| 126 | no lib LIST; |
| 127 | |
| 128 | =head1 DESCRIPTION |
| 129 | |
| 130 | This is a small simple module which simplifies the manipulation of @INC |
| 131 | at compile time. |
| 132 | |
| 133 | It is typically used to add extra directories to perl's search path so |
| 134 | that later C<use> or C<require> statements will find modules which are |
| 135 | not located on perl's default search path. |
| 136 | |
| 137 | =head2 Adding directories to @INC |
| 138 | |
| 139 | The parameters to C<use lib> are added to the start of the perl search |
| 140 | path. Saying |
| 141 | |
| 142 | use lib LIST; |
| 143 | |
| 144 | is I<almost> the same as saying |
| 145 | |
| 146 | BEGIN { unshift(@INC, LIST) } |
| 147 | |
| 148 | For each directory in LIST (called $dir here) the lib module also |
| 149 | checks to see if a directory called $dir/$archname/auto exists. |
| 150 | If so the $dir/$archname directory is assumed to be a corresponding |
| 151 | architecture specific directory and is added to @INC in front of $dir. |
| 152 | |
| 153 | To avoid memory leaks, all trailing duplicate entries in @INC are |
| 154 | removed. |
| 155 | |
| 156 | =head2 Deleting directories from @INC |
| 157 | |
| 158 | You should normally only add directories to @INC. If you need to |
| 159 | delete directories from @INC take care to only delete those which you |
| 160 | added yourself or which you are certain are not needed by other modules |
| 161 | in your script. Other modules may have added directories which they |
| 162 | need for correct operation. |
| 163 | |
| 164 | The C<no lib> statement deletes all instances of each named directory |
| 165 | from @INC. |
| 166 | |
| 167 | For each directory in LIST (called $dir here) the lib module also |
| 168 | checks to see if a directory called $dir/$archname/auto exists. |
| 169 | If so the $dir/$archname directory is assumed to be a corresponding |
| 170 | architecture specific directory and is also deleted from @INC. |
| 171 | |
| 172 | =head2 Restoring original @INC |
| 173 | |
| 174 | When the lib module is first loaded it records the current value of @INC |
| 175 | in an array C<@lib::ORIG_INC>. To restore @INC to that value you |
| 176 | can say |
| 177 | |
| 178 | @INC = @lib::ORIG_INC; |
| 179 | |
| 180 | =head1 CAVEATS |
| 181 | |
| 182 | In order to keep lib.pm small and simple, it only works with Unix |
| 183 | filepaths. This doesn't mean it only works on Unix, but non-Unix |
| 184 | users must first translate their file paths to Unix conventions. |
| 185 | |
| 186 | # VMS users wanting to put [.stuff.moo] into |
| 187 | # their @INC would write |
| 188 | use lib 'stuff/moo'; |
| 189 | |
| 190 | =head1 NOTES |
| 191 | |
| 192 | In the future, this module will likely use File::Spec for determining |
| 193 | paths, as it does now for Mac OS (where Unix-style or Mac-style paths |
| 194 | work, and Unix-style paths are converted properly to Mac-style paths |
| 195 | before being added to @INC). |
| 196 | |
| 197 | =head1 SEE ALSO |
| 198 | |
| 199 | FindBin - optional module which deals with paths relative to the source file. |
| 200 | |
| 201 | =head1 AUTHOR |
| 202 | |
| 203 | Tim Bunce, 2nd June 1995. |
| 204 | |
| 205 | =cut |