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

Is there any way to access the "raw" command-line in order to catch quoting and escaping?

The first google result for "windows api get command line" is GetCommandLineA. So I hunted on CPAN and thanks to Win32::API's samples I got:

use warnings; use strict; use feature 'state'; use Win32::API; sub GetCommandLine { state $GetCommandLine = Win32::API->new("kernel32", "GetCommandLin +e", [], 'P'); ( my $c = pack("a1024", $GetCommandLine->Call()) ) =~ s/\0*$//; return $c; } use Data::Dumper; $Data::Dumper::Useqq=1; my $cmdline = GetCommandLine(); print Dumper($cmdline); $cmdline =~ s/^(?:\Q$^X\E|perl(?:\.exe|\.bat|\.cmd)?)\s+(?:\Q$0\E\s+)? +//; # hack!! print "some_cmd ", $cmdline, "\n"; __END__ X:\Perl>perl cmdline.pl "1 2" "3" 4 $VAR1 = "perl cmdline.pl \"1 2\" \"3\" 4"; some_cmd "1 2" "3" 4

Update:

PS: I don't think this question is overly Win specific, when creating a bash script on Linux I'd face the same problem.

On Linux, your Perl script could theoretically have been invoked by execvp, meaning there actually is no command line in the first place. I think you'd have to settle with using String::ShellQuote on @ARGV (note that it only supports bash style quoting).

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

Replies are listed 'Best First'.
Re^2: (Windows) verbatim command line arguments inside Perl script (updated)
by LanX (Saint) on Apr 16, 2021 at 17:29 UTC
    Awesome, thanks! :)

    I'll try this out and will give feedback.

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