perl 不加插值地执行系统命令

perl execute system command without interpolation

我想在我的 perl 脚本中调用 svn commit。以下代码似乎适用于 git bash,但不适用于 powershell:

$task_number = $ARGV[0];
$comment     = $ARGV[1];
print(`svn commit -m '#$task_number $comment' 2>&1`);

当在 powershell 中 运行 时,我得到类似

的错误

svn: E200009: '$MY_WORKING_DIRECTORY$comment' is not under version control

其中 $comment 就是我在 perl 脚本中得到的内容。看来powershell做了一些插值。我应该如何更改我的脚本来解决这个问题?提前致谢。

反引号使用 cmd,而不是 Power Shell。

在使用 sh 的 Perl 版本上(包括 macOS、MSYS 和 Cygwin),您应该使用

use String::ShellQuote qw( shell_quote );

my $cmd = shell_quote('svn', 'commit', '-m', "#$task_number $comment") . ' 2>&1';
`$cmd`

在使用 cmd 的 Perl 构建中,您应该使用

use Win32::ShellQuote qw( quote_system_cmd );

my $cmd = quote_system_cmd('svn', 'commit', '-m', "#$task_number $comment") . ' 2>&1';
`$cmd`