perl 中的 chdir 使用两个字符串:chdir "/home/$dir1/$dir2"

chdir in perl using two strings: chdir "/home/$dir1/$dir2"

我有两个字符串:$dir1$dir2。我想使用 chdir 如下所示:

chdir "/home/$dir1/$dir2";

当我尝试这样做时,它不会更改目录。我检查了当前工作目录和以前一样

有什么办法吗?

通常我会这样写:

use v5.10;
use File::Spec::Functions;

my $dir = catfile( '/home', $dir1, $dir2 );
die "Dir <$dir> isn't a directory or doesn't exist" unless -e -d $dir;
chdir $dir or die "Could not change to <$dir>: $!";

每当您对系统进行操作时,请检查结果以确保它发生了。

而且,奇怪的是,我没有意识到我书中的 Pearson 示例章节 Effective Perl Programming is the one that covers stacked file test operators