如何在 Perl 中查找用户的主目录?
我需要检查用户主目录中的文件是否存在,因此使用文件检查:
if ( -e "~/foo.txt" ) {
print "yes, it exists!" ;
}
即使用户主目录下有一个名为 foo.txt 的文件,Perl 总是 抱怨没有这样的文件或目录。当我用 /home/jimmy 替换“~”时(假设用户是 jimmy),Perl 会给出正确的判断。
你能解释一下为什么“~”在 Perl 中不起作用并告诉我 Perl 的方法是什么吗 找到用户的主目录?
I need to check whether a file in a user's home directory exists so use file check:
if ( -e "~/foo.txt" ) {
print "yes, it exists!" ;
}
Even though there is a file called foo.txt under the user's home directory, Perl always
complains that there is no such file or directory. When I replace "~" with /home/jimmy (let's say the user is jimmy) then Perl give the right verdict.
Could you explain why "~" dosen't work in Perl and tell me what is Perl's way to
find a user's home directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
~
是一个bash
-ism 而不是perl
-ism,这就是它不起作用的原因。鉴于您似乎使用的是 UNIX 类型的系统,可能最简单的解决方案是使用$HOME
环境变量,例如:是的,我知道用户可以更改他们的
$HOME
环境变量,但我会假设他们知道他们在做什么。如果没有,他们应得的一切:-)如果你想以正确的方式做到这一点,你可以查看File::HomeDir,它更懂平台。您可以在以下脚本 chkfile.pl
和脚本中看到它的运行情况:
~
is abash
-ism rather than aperl
-ism, which is why it's not working. Given that you seem to be on a UNIX-type system, probably the easiest solution is to use the$HOME
environment variable, such as:And yes, I know the user can change their
$HOME
environment variable but then I would assume they know what they're doing. If not, they deserve everything they get :-)If you want to do it the right way, you can look into File::HomeDir, which is a lot more platform-savvy. You can see it in action in the following script
chkfile.pl
:and transcript:
我不确定每个人是如何错过 File::HomeDir 的。这是听起来很容易的艰巨任务之一,因为没有人知道你必须考虑的所有愚蠢的例外情况。它没有附带 Perl,因此您需要自己安装。
知道主目录后,使用 File::Spec 构建所需的路径:
I'm not sure how everyone missed File::HomeDir. It's one of those hard tasks that sound easy because no one knows about all of the goofy exceptions you have to think about. It doesn't come with Perl, so you need to install it yourself.
Once you know the home directory, construct the path that you need with File::Spec:
您可以glob波浪号,
glob('~/foo.txt')
应该可以工作。或者您可以使用 File::Save::Home 模块还要照顾其他系统。You can glob the tilde,
glob('~/foo.txt')
should work. Or you can use the File::Save::Home module that should also take care of other systems.用户的主目录存储在
/etc/passwd
中。获取信息的最佳方式是getpw*
函数:要解决您的具体问题,请尝试以下代码:
The home directory for a user is stored in
/etc/passwd
. The best way to get at the information is thegetpw*
functions:To address your specific problem, try this code:
尝试
File::Path::Expand
。Try
File::Path::Expand
.快进到 2020 年,从版本 5.31.10 开始,perl 中现在有一个名为 File 的模块: :Glob 解析波浪号,例如:
会产生:
/home/john
或
会产生:
/home/jack/some/folder
文件: :Glob 模块自 perl v5.6 起就存在,但仅与已弃用的
glob
子例程一起使用。Fast forward to 2020 and there is now in perl since version 5.31.10 at its core a module called File::Glob which resolves the tilde, such as:
would produce:
/home/john
or
would produce:
/home/jack/some/folder
The File::Glob module exist since perl v5.6, but only with the
glob
subroutine which has been deprecated.