Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by LanX (Saint)
on Apr 16, 2021 at 14:53 UTC ( [id://11131384]=note: print w/replies, xml ) Need Help??


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

Thanks, I was hoping for such modules ... :)

... but

> The quoting gets lost when the shell parses the command line arguments.

AFAIK that's true on Linux but not on Win

quoting Win32::ShellQuote

> Windows passes its arguments as a single string instead of an array as other platforms do. In almost all cases, the standard Win32 CommandLineToArgvW function is used to parse this string

So the win-built of Perl is deconstructing the input-line into @ARGV. (we had a similar discussion not long ago)

Hence it's not unthinkable to have a special var akin to %* in Perl to access that original line.

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

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

Replies are listed 'Best First'.
Re^3: (Windows) verbatim command line arguments inside Perl script
by haj (Vicar) on Apr 16, 2021 at 15:45 UTC

    Yeah, I noticed (too late) that I failed to correct that sentence after reading the docs of Win32::ShellQuote. Sorry.

Re^3: (Windows) verbatim command line arguments inside Perl script
by jcb (Parson) on Apr 16, 2021 at 22:28 UTC

    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.

      > 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?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found