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

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

by haukex (Bishop)
on Apr 16, 2021 at 15:13 UTC ( #11131385=note: print w/replies, xml ) Need Help??


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 (Cardinal) 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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2021-05-13 01:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (134 votes). Check out past polls.

    Notices?