Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Calling Windows Batch from Perl, double-quote appearing out of nowhere (insanity)

by tye (Sage)
on Nov 09, 2012 at 15:07 UTC ( [id://1003154]=note: print w/replies, xml ) Need Help??


in reply to Calling Windows Batch from Perl, double-quote appearing out of nowhere

People expect to be able to run system("dir *.foo") from Perl. But there is no 'dir' executable so to get that to work, Perl needs to actually run: cmd /c "dir *.foo"

So what Perl ran for you was:

C:\> cmd /c "cmd /c systemQuote.bat AA 1" C:\> echo XXXXXXX 1>X_1".log 2>&1 The filename, directory name, or volume label syntax is incorrect.

Which shows that Microsoft has a hard time dealing with quotes in a consistent or even sane and error-free manner. But I tried this:

C:\> cmd /c "cmd /c systemQuote.bat AA 1 " C:\> echo XXXXXXX 1>X_1.log 2>&1 C:\> sleep 5

And putting a trailing space also worked around the problem when running that from Perl.

It used to be that Perl's system() on Windows would try running the command you gave directly first and would only resort to wrapping it in qq(cmd /c "...") if that failed. I think that, over the years, enough edge cases showed up that the details of this technique were changed in order to try to get more "expected behavior" in more cases. If you are interested in details, then check out the source code involved, p5git://win32/win32.c..

- tye        

Replies are listed 'Best First'.
Re^2: Calling Windows Batch from Perl, double-quote appearing out of nowhere (insanity)
by rovf (Priest) on Nov 09, 2012 at 15:26 UTC
    Interesting point - this indeed explains (and it is also interesting that the quoting problem only shows up when we invoke the background version, but not the "normal" call to system).
    It used to be that Perl's system() on Windows would trying running the command you gave directly first and would only resort to wrapping it in qq(cmd /c "...") if that failed. I think that, over the years, enough edge cases showed up that the details of this technique were changed in order to try to get more "expected behavior" in more cases.
    Would this mean that in newer versions of Perl, the "foregroun" system() would show the same problem? I thought of using system() instead of system(1,...) and doing an explicit fork and exect, to avoid the quoting bug, but if newer Perl versions will consistently wrap the argument inside an extra invocation of cmd /c, the problem would reappear if I upgrade to a newer Perl :-(

    -- 
    Ronald Fischer <ynnor@mm.st>

      Just for y'all's information, I don't know the answer to that. It would be interesting to look into why only system(1,...) wraps things in cmd /c "..." in a way that triggers this bug. To figure that out, I'd read the code that I linked to above. I did scan some parts of it earlier but not looking for that quirk (I initially missed your point that the "1," was required).

      But why not just include a trailing space and then none of that will likely matter? (And include a comment noting that the trailing space is required to work around a bug in: cmd /c "cmd /c ...".)

      - tye        

        But why not just include a trailing space and then none of that will likely matter?

        Agreed! This indeed looks like the most sensible solution. (well, maybe the most sensible one would be to avoid Windows alltogether ;-)

        -- 
        Ronald Fischer <ynnor@mm.st>
Re^2: Calling Windows Batch from Perl, double-quote appearing out of nowhere (insanity)
by Anonymous Monk on Nov 09, 2012 at 15:29 UTC
    FWIW it actually ran
    $ perl -V:sh sh='cmd /x /c';

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-19 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found