http://qs321.pair.com?node_id=699036

rovf has asked for the wisdom of the Perl Monks concerning the following question:

My application generates at run time a Windows BAT file (oh you wonderful Windows Batch language - if I only knew which insane person had invented it!), which then in turn calls other programs (some compiled C application, some Perl programs). I need to pass strings from my Perl application via the Batch file to these programs (using the environment is no option here). For example:

my $some_argument='abc'; # ... $batchfile=IO::File->new(">x.bat"); print $batchfile "\@echo off\nMyProg $some_argument\n"; $batchfile->close; # ... later, in a different process ... : system("x.bat"); # executes MyProg abc
That's the basic idea. Of course it is not so easy, because I don't know the content of $some_argument until at run-time, and this means I have to generate the argument in a way which is properly quoted according to Batch Language Syntax Rules.

I researched a bit how to do proper quoting in Windows batch files, and though a found a bit of information here and a bit of information there, I could not find a concise document which really describes it properly. So my first question is:

Does someone happen to know a CPAN module which implements Windows Batch Language quoting? Otherwise, does someone know the rules, so that I can implement it myself? So far, I found the following set of rules:

  1. The special characters <>|^ must be escaped by ^ (for example, we have to convert 'a^b|c' into 'a^^b^|c')
  2. A double quote at the beginning or at the end of the argument must be escaped by \ (for example, we have to convert '"ab"' to '\\"ab"\\')
  3. If the argument contains spaces, it must be enclosed by double quotes, and in practice, it does not hurt to enclose the argument in double quotes always (for example, we have to convert 'a b' to '"a b"')
  4. A double quote, which is followed by a space, must get a backslash in front (for example, we have to convert 'a" b' to 'a\" b')
  5. If the argument starts with \", we are out of luck (at least I have not found yet a way how to encode the string '\\"foo' properly for my batchfile
Is this list complete or do I miss something?

-- 
Ronald Fischer <ynnor@mm.st>