如何使Perl识别带有'〜'的路径? [英] How can I make Perl recognize paths with '~'?

查看:103
本文介绍了如何使Perl识别带有'〜'的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何在Perl中找到用户的主目录?

Possible Duplicate:
How do I find a user's home directory in Perl?

我正在运行Ubuntu.

I'm running Ubuntu.

每当我向Perl脚本传递以~开头的路径(例如~/Documents/file.txt)时,它都会找不到它.我必须通过规范路径(例如/home/dave/Documents/file.txt).

Whenever I pass a Perl script a path that starts with ~ (e.g. ~/Documents/file.txt) it fails finding it. I must pass the canonical path (e.g. /home/dave/Documents/file.txt).

那是为什么?

我可以让Perl识别~路径吗?

Can I make Perl recognize ~ paths?

更新

所有建议的解决方案包括更改脚本中的代码.我想要一个不会对脚本本身进行任何更改的解决方案(因为并非所有脚本都是我的).也许是Bash起作用的方式吗?

All the suggested solutions include changing the code in the scripts. I would like for a solution that would not involve any changes to the scripts themselves (since not all of them are mine). Perhaps something in the way Bash works?

问题的更新版本已发布在超级用户上.

推荐答案

有关可靠(且简便)的跨平台方法,请尝试

For a reliable (and easy) cross-platform method, try File::HomeDir:

use File::HomeDir;
print File::HomeDir->my_home;

这篇关于如何使Perl识别带有'〜'的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆