我在 Windows 上:-/在我的脚本中我有:
$ENV{'Powmig Path'}powermt
那给我:
C:\Program\ Files\EMC\PowerPath\powermt
如果我执行 if(-e $ENV{'Powmig Path'}powermt)
它不起作用。
我尝试用一些替换来改变我的路径\/
我也尝试添加更多双引号,但似乎没有任何效果:-(
例子:
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
if($^O =~ m/^MSWin32$/){
my $tmp = File::Spec->catdir($ENV{'Powmig Path'}, "powermt");
if(-e "\"$tmp\""){
print "powermt found\n";
}else{
print "No multipathing found \"$tmp\"\n";
}
$tmp =~ s/\\/\//g;
if(-e "\"$tmp\""){
print "powermt found\n";
}else{
print "No multipathing found \"$tmp\"\n";
}
}else{
print "Error: Unknow OS\n";
}
exit;
输出:
C:\Users\sgargasson\Desktop>perl test.pl
No multipathing found "C:\Program Files\EMC\PowerPath\powermt"
No multipathing found "C:/Program Files/EMC/PowerPath/powermt"
尝试不同的文件后,问题来自空间...
有人可以帮帮我吗?
致谢
最佳答案
您确实意识到您不能只在源代码中键入一个字符串,对吧?你需要引用它:
print "$ENV{'Powmig Path'}powermt";
...
if (-e "$ENV{'Powmig Path'}powermt")
这将对变量进行插值,在本例中是来自散列 %ENV
的散列值,并将其与字符串 powermt
连接。
如果您尝试将一个字符串连接到一个变量,您首先需要引用它,然后使用 operator将其附加到变量:
my $string = $ENV{'Powmig Path'} . "powermt";
# ^--- concatenation operator
但是,如果您正在尝试构建路径,则可以使用适合该任务的模块,例如 File::Spec
:
use strict;
use warnings;
use File::Spec;
my $path = File::Spec->catdir($ENV{'Powmig Path'}, "powermt");
关于windows - perl if( -e "带空格路径的窗口){},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21118633/