| 1 | ######################################################################## | ||
| 2 | # housekeeping | ||
| 3 | ######################################################################## | ||
| 4 | package FindBin::Bin v2.0.1; | ||
| 5 | use v5.40; | ||
| 6 | |||
| 7 | use parent qw( Exporter ); | ||
| 8 | |||
| 9 | use File::stat; | ||
| 10 | |||
| 11 | use Cwd qw( abs_path getcwd ); | ||
| 12 | use Symbol qw( qualify_to_ref ); | ||
| 13 | |||
| 14 | use File::Spec::Functions | ||
| 15 | qw | ||
| 16 | ( | ||
| 17 | catdir | ||
| 18 | catpath | ||
| 19 | curdir | ||
| 20 | file_name_is_absolute | ||
| 21 | rel2abs | ||
| 22 | rootdir | ||
| 23 | splitdir | ||
| 24 | splitpath | ||
| 25 | ); | ||
| 26 | |||
| 27 | sub pwd() | ||
| 28 | { | ||
| 29 | # preserve symlinks in the path as with | ||
| 30 | # "pwd -L" while still working on MSW, VMS. | ||
| 31 | |||
| 32 | if( $^O eq 'MSWin32' ) | ||
| 33 | { | ||
| 34 | # getcwd does not resolve junctions or symlinks here, and | ||
| 35 | # there is no usable inode to validate $PWD against. | ||
| 36 | |||
| 37 | getcwd | ||
| 38 | } | ||
| 39 | elsif( $^O eq 'VMS' ) | ||
| 40 | { | ||
| 41 | # $ENV{DEFAULT} keeps rooted logicals unexpanded. | ||
| 42 | |||
| 43 | $ENV{ DEFAULT } // getcwd | ||
| 44 | } | ||
| 45 | else | ||
| 46 | { | ||
| 47 | # *NIX, Cygwin, MSYS: trust $PWD only if it | ||
| 48 | # still names the current directory (same | ||
| 49 | # check as "pwd -L"). otherwise live with | ||
| 50 | # cwd and having rel2abs resolve symlinks. | ||
| 51 | |||
| 52 | if( my $pwd = $ENV{ PWD } ) | ||
| 53 | { | ||
| 54 | if( $pwd =~ m{^/} ) | ||
| 55 | { | ||
| 56 | my $env = File::stat::stat $pwd ; | ||
| 57 | my $cur = File::stat::stat '.' ; | ||
| 58 | |||
| 59 | $env->ino | ||
| 60 | && $cur->ino | ||
| 61 | && $env->dev == $cur->dev | ||
| 62 | && $env->ino == $cur->ino | ||
| 63 | ? $pwd | ||
| 64 | : getcwd | ||
| 65 | } | ||
| 66 | else | ||
| 67 | { | ||
| 68 | getcwd | ||
| 69 | } | ||
| 70 | } | ||
| 71 | else | ||
| 72 | { | ||
| 73 | getcwd | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | sub canon_dir( $dir ) | ||
| 79 | { | ||
| 80 | # notice that this ignores any volume, the caller | ||
| 81 | # can deal with it. this also expects a dir, not a | ||
| 82 | # file, path. | ||
| 83 | |||
| 84 | $dir or return; | ||
| 85 | |||
| 86 | my $raw | ||
| 87 | = file_name_is_absolute( $dir ) | ||
| 88 | ? $dir | ||
| 89 | : catdir pwd, $dir | ||
| 90 | ; | ||
| 91 | |||
| 92 | # caller gets back the path. | ||
| 93 | |||
| 94 | if( $^O eq 'VMS' ) | ||
| 95 | { | ||
| 96 | $raw | ||
| 97 | } | ||
| 98 | else | ||
| 99 | { | ||
| 100 | # MSW and *NIX have the same process: | ||
| 101 | # clean up '.' and '..' in the path without | ||
| 102 | # calling rel2abs to resolve the symlinks. | ||
| 103 | |||
| 104 | state $root_marker = ( splitdir rootdir )[ 0 ]; | ||
| 105 | |||
| 106 | my @dirz = splitdir $raw; | ||
| 107 | my @stack = (); | ||
| 108 | |||
| 109 | while( @dirz ) | ||
| 110 | { | ||
| 111 | my $part = shift @dirz; | ||
| 112 | |||
| 113 | next if $part eq q{.}; | ||
| 114 | |||
| 115 | if( $part eq q{..} ) | ||
| 116 | { | ||
| 117 | pop @stack | ||
| 118 | if @stack && $stack[ -1 ] ne $root_marker; | ||
| 119 | } | ||
| 120 | else | ||
| 121 | { | ||
| 122 | push @stack, $part; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | catdir @stack | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | BEGIN | ||
| 131 | { | ||
| 132 | # recall that $0 is an lvalue!! | ||
| 133 | # grab it quickly before it can change. | ||
| 134 | # this will not change during execution, compute it once. | ||
| 135 | # | ||
| 136 | # note that $0 is initialized to a file path, not a dir. | ||
| 137 | |||
| 138 | my ( $logical, $physical ) | ||
| 139 | = do | ||
| 140 | { | ||
| 141 | if( $0 =~ m{^ (?: -e | - | interactive ) }x ) | ||
| 142 | { | ||
| 143 | my $raw = pwd; | ||
| 144 | |||
| 145 | ( | ||
| 146 | $raw | ||
| 147 | , rel2abs( $raw ) | ||
| 148 | ) | ||
| 149 | } | ||
| 150 | else | ||
| 151 | { | ||
| 152 | my $phys | ||
| 153 | = do | ||
| 154 | { | ||
| 155 | my ( $v, $d ) = ( splitpath abs_path( $0 ), '' )[0,1]; | ||
| 156 | catpath $v, catdir( $d ), '' | ||
| 157 | }; | ||
| 158 | |||
| 159 | my $log | ||
| 160 | = do | ||
| 161 | { | ||
| 162 | my ( $v, $d ) = ( splitpath $0, '' )[0,1]; | ||
| 163 | catpath $v, canon_dir( $d ), '' | ||
| 164 | }; | ||
| 165 | |||
| 166 | ( $log, $phys ) | ||
| 167 | } | ||
| 168 | }; | ||
| 169 | |||
| 170 | # notice that these are installed into the *local* package. | ||
| 171 | # callers inherit them via import(), below. | ||
| 172 | |||
| 173 | # $Bin_l -- logical path (canon_dir, like "pwd -L") | ||
| 174 | |||
| 175 | eval qq| *{ qualify_to_ref 'Bin_l' } = \\'$logical' |; | ||
| 176 | eval qq| sub Bin_l(){ '$logical' } |; | ||
| 177 | |||
| 178 | # $Bin_p -- physical path (abs_path of $0, resolves all symlinks) | ||
| 179 | |||
| 180 | eval qq| *{ qualify_to_ref 'Bin_p' } = \\'$physical' |; | ||
| 181 | eval qq| sub Bin_p(){ '$physical' } |; | ||
| 182 | |||
| 183 | # $Bin -- module-level default = logical; import() may | ||
| 184 | # alias to physical | ||
| 185 | |||
| 186 | eval qq| *{ qualify_to_ref 'Bin' } = \\'$physical' |; | ||
| 187 | eval qq| sub Bin(){ '$physical' } |; | ||
| 188 | } | ||
| 189 | |||
| 190 | ######################################################################## | ||
| 191 | # package variables & sanity checks | ||
| 192 | ######################################################################## | ||
| 193 | |||
| 194 | our @EXPORT = qw( $Bin Bin ); | ||
| 195 | our @EXPORT_OK = qw( $Bin Bin $Bin_l Bin_l $Bin_p Bin_p pwd canon_dir ); | ||
| 196 | |||
| 197 | ######################################################################## | ||
| 198 | # interface | ||
| 199 | ######################################################################## | ||
| 200 | |||
| 201 | sub import | ||
| 202 | { | ||
| 203 | my $pkg = shift; | ||
| 204 | my $caller = caller; | ||
| 205 | |||
| 206 | # list context captures the matched string, not a count. | ||
| 207 | my ($flag) = grep { m{abspath} } @_; | ||
| 208 | $flag //= ''; | ||
| 209 | my @want = grep { ! m{abspath} } @_; | ||
| 210 | |||
| 211 | my $use_phys | ||
| 212 | = $flag eq 'noabspath' | ||
| 213 | ? '' | ||
| 214 | : 1 | ||
| 215 | ; | ||
| 216 | |||
| 217 | # No explicit requests -- use default @EXPORT list. | ||
| 218 | |||
| 219 | @want = @EXPORT | ||
| 220 | unless @want; | ||
| 221 | |||
| 222 | if( grep { m{ ^ [&\$]? Bin $ }x } @want ) | ||
| 223 | { | ||
| 224 | my $flavor = $use_phys ? 'Bin_p' : 'Bin_l'; | ||
| 225 | my $src = qualify_to_ref $flavor => __PACKAGE__; | ||
| 226 | my $dst = qualify_to_ref 'Bin' => $caller; | ||
| 227 | |||
| 228 | # Alias $Bin (SCALAR slot) -- preserves read-only constraint. | ||
| 229 | *{ $dst } = *{ $src }{ SCALAR }; | ||
| 230 | |||
| 231 | # Alias Bin() (CODE slot) when the bare sub was requested. | ||
| 232 | *{ $dst } = *{ $src }{ CODE } | ||
| 233 | if grep { m{ ^ [&]? Bin $ }x } @want; | ||
| 234 | |||
| 235 | @want = grep { ! m{ ^ [&\$]? Bin $ }x } @want; | ||
| 236 | } | ||
| 237 | |||
| 238 | $pkg->export_to_level( 1, $pkg, @want ) | ||
| 239 | if @want; | ||
| 240 | } | ||
| 241 | |||
| 242 | 1 | ||
| 243 | __END__ |