Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^3: Problem with 'system' function in Windows Vista

by ikegami (Pope)
on Mar 20, 2007 at 19:19 UTC ( #605733=note: print w/ replies, xml ) Need Help??

in reply to Re^2: Problem with 'system' function in Windows Vista
in thread Problem with 'system' function in Windows Vista

Yes and no.

  • No, system does quote (in ActivePerl, at least).

    @cmd = ( 'echo', 'identifier', 'a string', '"pre quoted"' ); system(@cmd); # identifier "a string" "pre quoted" $cmd = "@cmd"; system($cmd); # identifier a string "pre quoted"
  • Yes, sending the command as a string instead of a list of arguments is a good idea in Windows. In Windows, command line parsing is done by the child. That means every program has its own mechanism to quote and escape its parameters (*). In turn, that means that system (or the underlying library) must guess at how the child wants its arguments quoted and escaped.

    Avoid the guessing if possible and provide to the child what the child will receive.

* — If any! When quoting is supported, it's usually double quotes. As for an escaping mechanism, I don't remember ever seeing one, so good luck trying to pass a double quote to a program...

Comment on Re^3: Problem with 'system' function in Windows Vista
Select or Download Code
Re^4: Problem with 'system' function in Windows Vista
by diotalevi (Canon) on Mar 20, 2007 at 23:32 UTC

    Escaping, when possible was usually just another doublequote: "bar ""baz" -> bar " baz.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      The documented MS C RTL quoting rules (that not all Win32 programs follow, unfortunately) say that to get <bar " baz> you should write <"bar \" baz">. The implementation is a bit weird, especially if you don't follow those rules. But your case doesn't work for me:

      C:\> perl -le"print '<',$_,'>' for @ARGV" "foo "" bar" <foo "> <bar> C:\>

      Note that \ only "escapes" when in a construct matching /\\+"/ (an even number of backslases results in $n-1 backslashes, an odd number in $n-1 backslashes followed by a quote).

      This pointed out a subtle bug in the "current" Perl quoting that is done:

      system( $^X, '"-leprint qq(<$_>) for @ARGV"', "\\root\\subdir\\", "\\root dir\\sub dir\\", ); produces: <\root\subdir\> <\root dir\sub dir">

      The fact that I had to quote and be careful with that second argument is the not-subtle short-coming. But the fix for it also fixes the final \ turning into a ".

      - tye        

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://605733]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2014-07-14 05:18 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (255 votes), past polls