Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: (Windows) verbatim command line arguments inside Perl script

by jcb (Parson)
on Apr 16, 2021 at 22:28 UTC ( [id://11131395] : note . print w/replies, xml ) Need Help??


in reply to Re^2: (Windows) verbatim command line arguments inside Perl script
in thread (Windows) verbatim command line arguments inside Perl script

If I understand correctly, it is even more convoluted than that: cmd.exe does perform some substitutions, so there really is no way to get the exact command line, even though Windows provides a "command tail" instead of an argument vector. The Windows build of perl does nothing special here — the C runtime library startup code parses the command tail and builds an argument vector that perl then packages into @ARGV.

  • Comment on Re^3: (Windows) verbatim command line arguments inside Perl script
  • Download Code

Replies are listed 'Best First'.
Re^4: (Windows) verbatim command line arguments inside Perl script
by LanX (Saint) on Apr 17, 2021 at 12:40 UTC
    > cmd.exe does perform some substitutions,

    If you are referring to %variables% , that's fine.

    I only need to be able to reproduce the exact call inside a Perl wrapper.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      In that case, using GetCommandLine as haukex suggested will work for the common case. I do not know enough about cmd.exe quoting to say what will happen with "odd" inputs, but I vaguely remember reading an explanation of Windows shell quoting somewhere that mentioned that ^ was somehow special, but not removed and therefore not useful as a quote character. I got the impression that there are no predictable rules in Windows shell quoting.

      The main problem I foresee in edge cases is the possibility that cmd.exe will further interpret your command tail if given a chance to read it again, as will occur if you write it out in a batch file. For example, as I recall, double quotes are quoted within double-quoted text by doubling them: "foo""bar" is passed as foo"bar. Now consider saving the command tail if "foo""""bar" is given, which will arrive as foo""bar and be reduced to foobar if cmd.exe gets to read it again and I understand correctly. Perhaps it is the C runtime startup code that actually does quote removal, in which case the command tail as returned from GetCommandLine will still have the original quotes.

      Could there be another way to solve this? If you just need to rerun your script with the same @ARGV, could you serialize @ARGV somehow (Storable, YAML, JSON, ...) and have a --recover-arguments FILE option that just reads the file and overwrites @ARGV?