简体   繁体   English

如何在Windows上的Perl中访问带有空格的路径?

[英]How do I access paths with spaces in them in Perl on Windows?

I'm converting a Unix Perl script to run on Windows. 我正在将Unix Perl脚本转换为在Windows上运行。 I'm having a problem with paths that have spaces in them: 我遇到了包含空格的路径的问题:

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,

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。

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. 您可能还对qq()q()运算符感兴趣,它们允许您使用除引号之外的分隔符来分隔字符串。 They are very helpful when you want to quote a string that includes quotes: 当您想引用包含引号的字符串时,它们非常有用:

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'

)

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

这是Perl,所以有1000种方式(如你所见),单向(逃避空间)

open (IN, "| C:\\Program\ Files\\MySQL\\MySQL\ Server\ 5.1\\bin\\mysql.exe -u root -ppwd") or die "$!";

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理所当然地指出,我有我的\\"放错了地方25年DOS的,我想念是:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM