如何在给定文件路径的情况下获取第一个现有的祖先目录?

发布于 2024-12-04 22:27:38 字数 486 浏览 1 评论 0原文

在 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/ 不存在,但目录 < code>/home/leguri/images/ 确实如此,结果应该是 /home/leguri/images/

是否有一个模块或函数可以执行此操作,或者只是分割 / 上的路径并测试是否存在?

In Perl, if given a file path, how do you find the first existing ancestor?

For example:

  • If given the path /opt/var/DOES/NOT/EXIST/wtv/blarg.txt and directory /opt/var/DOES/ is not present but directory /opt/var/ is, the result should be /opt/var/.
  • If given the path /home/leguri/images/nsfw/lena-full.jpg and directory /home/leguri/images/nsfw/ does not exist, but directory /home/leguri/images/ does, the result should be /home/leguri/images/.

Is there a module or function which does this, or is it just a matter of splitting the path on / and testing for existence?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一瞬间的火花 2024-12-11 22:27:38

我知道的最接近的是 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;

The closest I know of is Path::Class, which doesn't do exactly what you want but may save you a couple of steps in splitting the path.

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;
青衫负雪 2024-12-11 22:27:38

猴子修补它

#!/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

Monkey patch it

#!/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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文