如何使Perl识别带有'〜'的路径? [英] How can I make Perl recognize paths with '~'?
问题描述
可能重复:
如何在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屋!