Linux 为什么我的Perl脚本在“~/”上失败,而在“$ENV{HOME}”上工作?
我一直在使用我的这个脚本,我一直在使用~/来扩展我的主目录。我今天开始工作,但它停止了工作:Linux 为什么我的Perl脚本在“~/”上失败,而在“$ENV{HOME}”上工作?,linux,perl,unix,Linux,Perl,Unix,我一直在使用我的这个脚本,我一直在使用~/来扩展我的主目录。我今天开始工作,但它停止了工作: #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 di
#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 : $!";
你知道为什么这会突然停止工作吗
错误看起来像:
cant open stephen/tmp/find_coverp.rslt : No such file or directory at /user/stephen/bin/find.pl line 137.
~由外壳扩展。Perl对此一无所知。因此,它只能在shell脚本中工作。~由shell扩展。Perl对此一无所知。因此,它只能在shell脚本中工作。tilde扩展不是由perl完成的,而是由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': $!";
tilde扩展不是由perl完成的,而是由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': $!";
正如前面的回答所述,~tilde是由shell扩展的,而不是由perl扩展的。 最有可能的原因是,当前目录中存在一个目录~而该目录最终被删除,导致出现错误: 举例说明: Tilde不在Perl中工作,使用$ENV{HOME}工作: $ echo MM > MM $ perl5.8 -e '{print `cat ~/MM`}' cat: cannot open ~/MM $ perl5.8 -e '{print `cat $ENV{HOME}/MM`}' MM 使波浪形命名目录生效: $ mkdir \~ $ echo MM > \~/MM $ ls -l \~ -rw-rw-r-- 1 DVK users 3 Jun 10 15:15 MM $ perl5.8 -e '{print `cat ~/MM`}' MM 如您所观察到的,删除它将恢复错误: $ /bin/rm -r \~ $ ls -l \~ ~: No such file or directory $ perl5.8 -e '{print `cat ~/MM`}' cat: cannot open ~/MM
这提供了一个似是而非的解释,虽然我不是100%,但不可能有其他解释。正如前面的回答所述,~tilde是由shell扩展的,而不是由perl扩展的。 最有可能的原因是,当前目录中存在一个目录~而该目录最终被删除,导致出现错误: 举例说明: Tilde不在Perl中工作,使用$ENV{HOME}工作: $ echo MM > MM $ perl5.8 -e '{print `cat ~/MM`}' cat: cannot open ~/MM $ perl5.8 -e '{print `cat $ENV{HOME}/MM`}' MM 使波浪形命名目录生效: $ mkdir \~ $ echo MM > \~/MM $ ls -l \~ -rw-rw-r-- 1 DVK users 3 Jun 10 15:15 MM $ perl5.8 -e '{print `cat ~/MM`}' MM 如您所观察到的,删除它将恢复错误: $ /bin/rm -r \~ $ ls -l \~ ~: No such file or directory $ perl5.8 -e '{print `cat ~/MM`}' cat: cannot open ~/MM
这提供了一个似是而非的解释,尽管我不是100%不可能有其他人。正如问题中所述,它一直在工作,今天突然停止工作……正如问题中所述,它一直在工作,今天突然停止工作……这些都是很好的答案,只是它一直在工作。那么,为什么它在某一点上起作用,以及有什么可以改变它,使它停止工作呢。为了证明它们是一样的,这里有一个与一周前不同的快照:>cp.snapshot/weekly.1/find.pl~/tmp>diff~/tmp/find.pl~/bin/find.pl 96c96