如何在 Windows 上的 Perl 中访问包含空格的路径? [英] How do I access paths with spaces in them in Perl on Windows?

查看:69
本文介绍了如何在 Windows 上的 Perl 中访问包含空格的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在转换一个 Unix Perl 脚本以在 Windows 上运行.我遇到了包含空格的路径的问题:

I'm converting a Unix Perl script to run on 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' 不是内部或外部命令,

我尝试用这样的转义 \" 包装:

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?

我使用的是为 MSWin32-x86-multi-thread 构建的 ActiveState v5.10.0.

I'm using ActiveState v5.10.0 built for MSWin32-x86-multi-thread.

推荐答案

您引用了整个命令,包括命令行参数.您应该在 mysql.exe 之后放置第二个转义引号:

You are quoting the entire command, including the command-line arguments. You should have put your second escaped quote after the mysql.exe:

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

您可能还对 qq()q() 运算符感兴趣,它们允许您使用引号以外的分隔符来分隔字符串.当您想引用包含引号的字符串时,它们非常有用:

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[| "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe" -u root -ppwd]

此外,Perl 会很乐意处理命令名称的正确路径分隔符(但并不总是用于命令参数,所以要小心):

Also, Perl will happily handle the correct path separator for command names (but not always for command arguments, so beware):

qq[| "C:/Program Files/MySQL/MySQL Server 5.1/bin/mysql.exe" -u root -ppwd]

(并且由于此示例不需要任何插值,您可以使用单引号或 q() 构造:

(And since this example doesn't need any interpolation, you could have used single-quotes or the q() construction:

'| "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe" -u root -ppwd'

)

这篇关于如何在 Windows 上的 Perl 中访问包含空格的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆