Passing quoted string to system () saving integers

This issue applies to Perl v5.24.0 on Windows 10

Except in the simplest cases. It is always a problem of passing commands and parameter lists between programs. Considering the effects of spaces and shell metacharacters, it is possible that the data remains intact at several levels of calls, may include a mess of screens and quotes

The panacea has always used the multiparameter form of system (which also tries to avoid invoking the shell as an intermediary), so that each parameter is reliably separated without the use of quotation marks

Call type

 system("dir \"C:\\Program Files\\\"") 

much simpler on an eye written like this:

 system('dir', 'C:\Program Files\\') 

However, I see no way to pass values โ€‹โ€‹containing enclosed quotes

If I write a test program

show.pl

 use Data::Dump; dd \@ARGV; 

and then call

 system('show', 'xxx') 

the conclusion that I get is what I expect

 ["xxx"] 

However, suppose I want to pass the string "xxx" . If i try

 system('show', '"xxx"') 

then the quotation marks are lost at some point along the way, and the result is identical to the previous example

How to make a system call so that the output is ["\"xxx\""] ?

I tried all kinds of shoots, but the solution evades me

+8
perl quotes exec
source share
1 answer

Problem:

 system($^X, '-E', 'say @ARGV', '"test"'); 

Output:

 test 

It is so broken! [one]


Decision:

 use Win32::ShellQuote qw( quote_system ); system(quote_system($^X, '-E', 'say @ARGV', '"test"')); 

Output:

 "test" 

  • Perl needs to create a command line even if the shell is not used. Unlike unix, where the system call for executing a program takes the path to the program and a list of arguments, the Windows system call for executing the program takes a command line, so you need to create a command line even if you avoid the shell. Perl seems to be building command lines incorrectly. [2] This is why using the syntax system BLOCK LIST does not help.

  • In fact, before the command line parsing application for arguments! Fortunately, there is a system call to form a standard.

+4
source share

All Articles