Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by jcb (Parson)
on Apr 18, 2021 at 02:19 UTC ( [id://11131418] : note . print w/replies, xml ) Need Help??


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

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?