Thursday, December 18, 2008

Perl, BAT files and quoteless quoting.

As much as I hate DOS batch files (*.BAT), sometimes you are forced to use them for integration with other build tools.

One frustration I had with a BAT file was the normal Unix rules for quoting, using nested single and double quotes, don't work.

For example, in unix (or Cygwin), you can do


perl -e 'print "Hello\n";'


But DOS doesn't know about single quotes. If you try


perl -e "print 'Hello\n';"


You will get


Hello\n%


... the newline is ignored since you used single quotes. (My version of Cygwin displays the % meaning that the output does not have an EOL character).

The work around is to use the rarely used "qq" perl functions to do quote-less quoting. Thus the following example


perl -e "print qq{Hello\n};"


prints out the correct message with the newline.


Hello

No comments: