如何使Perl识别带有'〜'的路径?

如何使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;