【发布时间】:2019-07-06 18:49:29
【问题描述】:
我有包含变量 $env->{'arguments'} 的 Perl 脚本,该变量应该包含一个 JSON 对象,我想将该 JSON 对象作为参数传递给我的其他外部脚本并使用反引号运行它。
转义前$env->{'arguments'}的值:
$VAR1 = '{"text":"This is from module and backslash \\ should work too"}';
转义后$env->{'arguments'}的值:
$VAR1 = '"{\\"text\\":\\"This is from module and backslash \\ should work too\\"}"';
代码:
print Dumper($env->{'arguments'});
escapeCharacters(\$env->{'arguments'});
print Dumper($env->{'arguments'});
my $command = './script.pl '.$env->{'arguments'}.'';
my $output = `$command`;
转义字符功能:
sub escapeCharacters
{
#$env->{'arguments'} =~ s/\\/\\\\"/g;
$env->{'arguments'} =~ s/"/\\"/g;
$env->{'arguments'} = '"'.$env->{'arguments'}.'"';
}
我想问你什么是正确的方法以及如何将该 JSON 字符串解析为有效的 JSON 字符串,我可以将其用作我的脚本的参数。
【问题讨论】:
-
另见String::ShellQoute。另一种避免将参数交给 shell(因此需要转义)的替代方法是将其写入临时文件,然后将临时文件的文件名作为参数传递给脚本
标签: json perl escaping character