猴子修补它
#!/usr/bin/perl --
use strict;
use warnings;
use Path::Class ;
my @paths = map dir($_) ,
map join( '/', $_, 1..6 ),
grep defined,
@ENV{qw/ WINDIR PROGRAMFILES TEMP HOME /},
'Q:/bogus/drive/and/path',
'QQ:/bogus/drive/bogusly/treated/as/file',
;
push @paths, dir('bogus/relative/path/becomes/dot');
push @paths, dir('bogus/relative/path/becomes/relative/to/cwd')->absolute;
for my $path ( @paths ) {
print $path, "\n\t=> ", $path->real_parent, "\n\n";
}
sub Path::Class::Entity::real_parent {
package Path::Class::Entity;
my( $dir ) = @_;
while( !-d $dir ){
my $parent = $dir->parent;
last if $parent eq $dir ; # no infinite loop on bogus drive
$dir = $parent;
}
return $dir if -d $dir;
return;
}
__END__
C:\WINDOWS\1\2\3\4\5\6
=> C:\WINDOWS
C:\PROGRA~1\1\2\3\4\5\6
=> C:\PROGRA~1
C:\DOCUME~1\bogey\LOCALS~1\Temp\1\2\3\4\5\6
=> C:\DOCUME~1\bogey\LOCALS~1\Temp
C:\DOCUME~1\bogey\1\2\3\4\5\6
=> C:\DOCUME~1\bogey
Q:\bogus\drive\and\path\1\2\3\4\5\6
=>
QQ:\bogus\drive\bogusly\treated\as\file\1\2\3\4\5\6
=> .
bogus\relative\path\becomes\dot
=> .
C:\temp\bogus\relative\path\becomes\relative\to\cwd
=> C:\temp