eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:
On Windows, the following C program:
when run like this:#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; }
prints:> arg.exe "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.0:arg.exe: 1:abc " xyz:
Notice that the following Perl program:
when run with the same command line arguments:for my $arg (@ARGV) { print "$arg:\n" }
prints instead:> perl arg.pl "abc "" xyz"
abc ": xyz:
I tried:
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).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";
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parsing Windows CommandLine from Perl
by NetWallah (Canon) on Apr 17, 2015 at 19:11 UTC | |
|
Re: Parsing Windows CommandLine from Perl
by fishmonger (Chaplain) on Apr 17, 2015 at 15:05 UTC | |
by davies (Monsignor) on Apr 18, 2015 at 10:38 UTC | |
|
Re: Parsing Windows CommandLine from Perl
by FreeBeerReekingMonk (Deacon) on Apr 17, 2015 at 20:07 UTC | |
by fishmonger (Chaplain) on Apr 17, 2015 at 20:30 UTC | |
by Anonymous Monk on Apr 17, 2015 at 20:53 UTC | |
|
Re: Parsing Windows CommandLine from Perl
by Anonymous Monk on Apr 17, 2015 at 19:07 UTC | |
|
Re: Parsing Windows CommandLine from Perl
by crusty_collins (Friar) on Apr 17, 2015 at 16:13 UTC | |
by fishmonger (Chaplain) on Apr 17, 2015 at 16:47 UTC | |
|
Re: Parsing Windows CommandLine from Perl
by kroach (Pilgrim) on Apr 17, 2015 at 17:23 UTC |