perl if(-e"带空格路径的窗口){} [英] perl if( -e "windows with space path){}
问题描述
我在 Windows 上:-/,在我的脚本中,我已经:
I'm on Windows :-/ and in my script i've:
$ENV{'Powmig Path'}powermt
那给我:
C:\Program\ Files\EMC\PowerPath\powermt
如果我执行if(-e $ENV{'Powmig Path'}powermt)
,则不起作用.
if I do a if(-e $ENV{'Powmig Path'}powermt)
it doesn't work.
我尝试用一些替代方式更改我的路径\/
I have try to change my path with some substitution \ /
我也尝试添加更多的双引号,但似乎无济于事:-(
I have also try to add more double quote but nothing seems to work :-(
示例:
#!/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"
尝试使用不同的文件后,问题出在空间...
After some try with different files, the problem comming from the space...
有人可以帮我吗?
高级助手
推荐答案
您确实意识到不能只在源代码中键入字符串,对吗?您需要引用它:
You do realize that you cannot just type a string into the source code, right? You need to quote it:
print "$ENV{'Powmig Path'}powermt";
...
if (-e "$ENV{'Powmig Path'}powermt")
这将对变量(在本例中为来自哈希%ENV
的哈希值)进行插值,并将其与字符串powermt
串联.
This will interpolate the variable, in this case a hash value from the hash %ENV
, and concatenate it with the string powermt
.
如果您确实尝试将字符串连接到变量,则首先需要将其引号,然后使用运算符将其附加到变量:
And if you do try to concatenate a string to a variable, you first need to quote it, and then use an operator to attach it to the variable:
my $string = $ENV{'Powmig Path'} . "powermt";
# ^--- concatenation operator
但是,如果您尝试构建路径,则可以使用适合该任务的模块,例如 File::Spec
:
If you are trying to build paths, though, you might use a module suitable for that task, such as File::Spec
:
use strict;
use warnings;
use File::Spec;
my $path = File::Spec->catdir($ENV{'Powmig Path'}, "powermt");
这篇关于perl if(-e"带空格路径的窗口){}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!