如何在 Windows 上的 Perl 中访问包含空格的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4597463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access paths with spaces in them in Perl on Windows?
提问by Kev
I'm converting a Unix Perl script to run on Windows. I'm having a problem with paths that have spaces in them:
我正在转换 Unix Perl 脚本以在 Windows 上运行。我在路径中有空格时遇到问题:
open (IN, "| C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe -u root -ppwd") or die "$!";
The code above throws the following error:
上面的代码抛出以下错误:
'C:\Program' is not recognized as an internal or external command,
'C:\Program' is not recognized as an internal or external command,
I tried wrapping in escaped \"
like this:
我尝试\"
像这样包装转义:
open (IN, "| \"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe -u root -ppwd\"") or die "$!";
But no joy. How do I handle paths with spaces?
但没有快乐。如何处理带有空格的路径?
I'm using ActiveState v5.10.0 built for MSWin32-x86-multi-thread.
我正在使用为 MSWin32-x86-multi-thread 构建的 ActiveState v5.10.0。
回答by mob
You are quoting the entire command, including the command-line arguments. You should have put your second escaped quote after the mysql.exe
:
您引用了整个命令,包括命令行参数。你应该把你的第二个转义引号放在mysql.exe
:
open (IN, "| \"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe\" -u root -ppwd") or die "$!";
You might also be interested in the qq()
and q()
operators, which allow you to use delimiters other than quotation marks to delimit strings. They are very helpful when you want to quote a string that includes quotes:
您可能还对qq()
andq()
运算符感兴趣,它允许您使用引号以外的分隔符来分隔字符串。当您想引用包含引号的字符串时,它们非常有用:
qq[| "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe" -u root -ppwd]
Also, Perl will happily handle the correct path separator for command names (but not always for command arguments, so beware):
此外,Perl 会很高兴地处理命令名称的正确路径分隔符(但并不总是用于命令参数,所以要小心):
qq[| "C:/Program Files/MySQL/MySQL Server 5.1/bin/mysql.exe" -u root -ppwd]
(And since this example doesn't need any interpolation, you could have used single-quotes or the q()
construction:
(并且由于此示例不需要任何插值,您可以使用单引号或q()
构造:
'| "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe" -u root -ppwd'
)
)
回答by Glen Solsberry
You have to escape the spaces as well.
你也必须逃离这些空间。
open (IN, "| C:\Program\ Files\MySQL\MySQL\ Server\ 5.1\bin\mysql.exe -u root -ppwd") or die "$!";
Or use the old 8.3 names:
或者使用旧的8.3 名称:
open (IN, "| C:\Progra~1\MySQL\MySQL~1\bin\mysql.exe -u root -ppwd") or die "$!";
Though, I have to question the sanity of piping the MySQL client, instead of just using DBI
虽然,我不得不质疑管道 MySQL 客户端的理智,而不仅仅是使用DBI
回答by Lou Franco
It's Perl, so there are a 1000 ways (as you'll see), One way (escape the spaces)
它是 Perl,所以有 1000 种方式(如您所见),一种方式(逃避空格)
open (IN, "| C:\Program\ Files\MySQL\MySQL\ Server\ 5.1\bin\mysql.exe -u root -ppwd") or die "$!";
回答by Kev
My solution was to do this:
我的解决方案是这样做:
$mysql = "C:\Program\ Files\MySQL\MySQL\ Server\ 5.1\bin\mysql.exe";
open (IN, "| \"$mysql\" -u root -ppwd") or die "$!";
Update:I also noticed that as @mob rightfully points out I had my \"
in the wrong place. Twenty five years of DOS and I miss that :/
更新:我也注意到@mob 正确地指出我\"
在错误的地方。DOS 25 年,我很怀念:/