为什么我的 Perl 脚本在“~/"上失败?但适用于“$ENV{HOME}"? [英] Why does my Perl script fail on "~/" but works with "$ENV{HOME}"?
本文介绍了为什么我的 Perl 脚本在“~/"上失败?但适用于“$ENV{HOME}"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在使用我的这个脚本,并且一直使用~/"来扩展我的主目录.我今天开始工作,但它停止工作了:
I have been using this script of mine FOREVER and I have always been using "~/" to expand my home directory. I get into work today and it stopped working:
#if ( $output eq "" ) { $output = "~/tmp/find_$strings[0].rslt" } # BROKEN
if ( $output eq "" ) { $output = "$ENV{HOME}/tmp/find_$strings[0].rslt" } #WORKS
...
open OUT_FILE, ">$output" or die "cant open $output : $!";
关于为什么这会突然停止工作的任何想法?
Any ideas about why this would suddenly stop worrking?
错误看起来像:
cant open stephen/tmp/find_coverp.rslt : No such file or directory at /user/stephen/bin/find.pl line 137.
推荐答案
波浪号扩展不是由 perl 完成的,而是由 shell 完成的.
The tilde expansion is not done by perl, it is done by the shell.
你应该使用:
use File::Spec::Functions qw( catfile );
...
my $fn = catfile $ENV{HOME}, 'tmp', "find_$strings[0].rslt";
...
open my $out, '>', $fn or die "Cannot open '$fn': $!";
这篇关于为什么我的 Perl 脚本在“~/"上失败?但适用于“$ENV{HOME}"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文