1

在 Perl 中,如果给定一个文件路径,你如何找到第一个存在的祖先?

例如:

  • 如果给定路径/opt/var/DOES/NOT/EXIST/wtv/blarg.txt和目录/opt/var/DOES/不存在但目录/opt/var/存在,则结果应为/opt/var/.
  • 如果给定的路径/home/leguri/images/nsfw/lena-full.jpg和目录/home/leguri/images/nsfw/不存在,但目录存在/home/leguri/images/,则结果应该是/home/leguri/images/.

是否有一个模块或功能可以做到这一点,或者只是分割路径/和测试存在的问题?

4

2 回答 2

5

我所知道的最接近的是Path::Class,它并不能完全按照您的意愿行事,但可能会在拆分路径时为您节省几个步骤。

use Path::Class 'dir';

sub get_existing_dir {
    my ( $path ) = @_;

    my $dir = dir( $path );
    while (!-d $dir) {
        $dir = $dir->parent;
    }
    return $dir->stringify;
}

my $file = '/opt/var/DOES/NOT/EXIST/wtv/blarg.txt';
my $dir = get_existing_dir( $file );
print $dir;
于 2011-09-17T03:10:51.617 回答
0

猴子修补它

#!/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
于 2011-09-17T13:38:33.667 回答