perl - Passing a quoted string to system() keeping quotes intact -
this problem applies perl v5.24.0 on windows 10
except simplest cases. problem passing command lines , parameter lists between programs. taking account effects of whitespace , shell metacharacters, possibly data remains intact on several levels of calls, can involve mess of escapes , quotation marks
the panacea has been use multiple-parameter form of system
(which tries avoid calling shell intermediary) each parameter reliably separated without resorting quotes
a call this
system("dir \"c:\\program files\\\"")
is easier on eye written this
system('dir', 'c:\program files\\')
however, can see no way pass values include enclosing quotes
if write test program
show.pl
use data::dump; dd \@argv;
and call
system('show', 'xxx')
then output expect
["xxx"]
however, suppose want pass string "xxx"
. if try
system('show', '"xxx"')
then quotes stripped @ point along way , output identical preceding example
how make call system
such output ["\"xxx\""]
?
i have tried manner of escaping, solution evades me
the problem:
system($^x, '-e', 'say @argv', '"test"');
output:
test
that's broken![1]
solution:
use win32::shellquote qw( quote_system ); system(quote_system($^x, '-e', 'say @argv', '"test"'));
output:
"test"
perl needs build command line if shell isn't used. unlike unix system call execute program takes path program , list of arguments, windows system call execute program takes command line, command line must built if shell avoided. appears perl builds command lines incorrectly.[2] why using
system block list
syntax doesn't help.in fact, it's application parse command line arguments! thankfully, there's system call that, standard has formed.
Comments
Post a Comment