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

Hi, I am trying to use my PERL script to write a DOS batch file, which can later be run from the command line. The problem is that the batch file output, which is essentially a text file, does not recognize the newline characters, and so is not formatted correctly.

Can anyone offer advice on how to write the output so that the results look like a properly formatted batch file?

my $outfile = "mybatchfile.bat"; open( OUTPUT, ">", $outfile) or die "Can't open file " . $outfile. " : + $!"; { print OUTPUT '@echo off\n setlocal\n set PATHNAME=C:\TEMP\%1_REGRESSION\n', 'set WAIT_TIME=240000\n set URL=\"http://myurl.biz\"\n', 'set PWD="megapassword"\n set EMAIL_TO_STRING="bob@test.com"; }
Output looks like:

@echo off\n setlocal\n set PATHNAME=C:\TEMP\%1_REGRESSION\n set WAIT_TIME=240000\n set URL=\"http://myurl.biz\"\n set PWD="megapassword"\n set EMAIL_TO_STRING="bob@test.com

instead of:
@echo off
setlocal
set PATHNAME=C:\TEMP\%1_REGRESSION
set WAIT_TIME=240000
set URL="http://myurl.biz"
set PWD="megapassword"
set EMAIL_TO_STRING="bob@test.com

Replies are listed 'Best First'.
Re: Using perl to output a DOS batch file
by johngg (Canon) on Oct 15, 2010 at 19:16 UTC
    You have used single quotes which don't allow interpolation of variables or escapes so you get a literal \n rather than a newline. Since you also want double quotes in your output it will be easiest to use quoting constructs, qq{ ... } for double quotes. You may also want to include carriage returns if the target is DOS, I'm not sure if Perl takes care of this for you if built for that platform. You will have to double up the backslashes and escape the % sign inside double quotes.

    my $outfile = q{mybatchfile.bat}; open( OUTPUT, q{>}, $outfile) or die qq{Can't open file $outfile : $!\n}; { print OUTPUT qq{\@echo off\n setlocal\n set PATHNAME=C:\\TEMP\\\%1_REGRESSIO +N\n}, qq{set WAIT_TIME=240000\n set URL="http://myurl.biz"\n}, qq{set PWD="megapassword"\nset EMAIL_TO_STRING="bob@test.com"}; }

    I hope this is helpful.

    Update: Thinking about it, your code might be a lot clearer if you use a HEREDOC.

    my $outfile = q{mybatchfile.bat}; open( OUTPUT, q{>}, $outfile) or die qq{Can't open file $outfile : $!\n}; print OUTPUT <<EOT @echo off setlocal set PATHNAME=C:\TEMP\%1_REGRESSION set WAIT_TIME=240000 set URL="http://myurl.biz" set PWD="megapassword" set EMAIL_TO_STRING="bob@test.com" EOT

    Also, I should have escaped the @ in the first code sample; corrected.

    Update: Corrected typo - s{qotes}{quotes}

    Cheers,

    JohnGG

Re: Using perl to output a DOS batch file
by halfcountplus (Hermit) on Oct 15, 2010 at 19:05 UTC

    Single quotes disable escape sequences.

    print "\"Hello world\"\n";

    Prints "Hello world" and a newline.

    print '\"Hello world\"\n';

    Prints \"Hello world\"\n.

    print '"Hello world"'."\n";

    Prints "Hello world" and a newline.

Re: Using perl to output a DOS batch file
by Anonymous Monk on Oct 15, 2010 at 19:21 UTC
    You don't need to escape double quotes appearing between single quotes, because the only escape sequences in single quotes are \' and \\.

    You can use another delimiter for your string to reduce confusion. eg, my $foo = 'bar'; qq{echo $foo\r\n};