On Windows, the following C program:

#include <stdio.h> int main(int argc, char* argv[]) { int i; for (i = 0; i < argc; ++i) { printf("%d:%s:\n", i, argv[i]); } return 0; }
when run like this:
> arg.exe "abc "" xyz"
prints:
0:arg.exe: 1:abc " xyz:
Though escaping double quotes inside a double quoted string by repeating them (as above) is ungainly, it is common in the Windows world and I need to support it.

Notice that the following Perl program:

for my $arg (@ARGV) { print "$arg:\n" }
when run with the same command line arguments:
> perl arg.pl "abc "" xyz"
prints instead:
abc ": xyz:

I tried:

use strict; use warnings; use Win32::API; my $getcmdline = Win32::API->new( 'kernel32.dll', 'GetCommandLine', [] +, 'P' ) or die "error: Win32::API GetCommandLine: $^E"; my $cmdline = pack 'Z*', $getcmdline->Call(); $cmdline =~ tr/\0//d; # remove any NULLs left over from pack Z* $cmdline =~ s/\s+$//; # remove trailing white space print "cmdline=$cmdline:\n";
to get at the Windows command line, but ran into the "random crashing problem" described at Win32::API Memory Exception with GetCommandLine() (which returns a static string).

It seems I'll need to use Win32::CommandLine (which I cannot currently get to build cleanly) or write a C front end to do the argument passing before launching Perl. Is there another way around this that I've missed?


In reply to Parsing Windows CommandLine from Perl by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.