如何在给定文件路径的情况下获取第一个现有的祖先目录?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道的最接近的是 Path::Class ,它并不完全正确你想要什么,但可能会节省你分割路径的几个步骤。
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.
猴子修补它
Monkey patch it