Hi

I need to write a Perl script which is generating a .bat-file while passing it's command line arguments 1-to-1

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

To highlight the problem:

D:\>perl -E"say qq(@ARGV)" "1 2" "3" 4 1 2 3 4

As you can see are all quotes and escapes lost while calling Perl (for good reasons)

But my script has to generate another .bat which does

... some_cmd "1 2" "3" 4 ...

Typical edge-case justifying this problem are win-paths with whitespace, but cmd.exe has also some arcane escaping rules.

I already have a workaround:

I discovered that .bat has %* to hold the verbatim command line arguments, and came up with a workaround of a .bat calling my perl-script

D:\tmp\exp>type cmd_wrapper.bat @echo off echo %* |perl -wE"chomp(my $args = <STDIN>);say qq(some_cmd $args)" D:\tmp\exp>cmd_wrapper.bat "1 2" "3" 4 some_cmd "1 2" "3" 4 D:\tmp\exp>

Question: any better solution?

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

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


In reply to (Windows) verbatim command line arguments inside Perl script by LanX

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.