SPUG: RE: One-liners on Windows via command.com

Aaron W. West tallpeak at hotmail.com
Wed Mar 23 20:04:51 PST 2005

Don't forget percent signs!

perldoc perlwin32 seems not to mention it:

In CMD.EXE or COMMAND.COM, percent signs in pairs are interpolated, when
determined to be a valid *and existing* variable name.

It seems the solution is a backslash before the percent sign.

It also appears that in a perl command line \cm means control-M (CR), and
\cj means control-J (LF). I don't know where this is documented.

Win XP Home:

C:\>echo %A%
C:\>echo %ComSpec%
C:\>perl -e "print \"%ComSpec%\""
C:\>perl -e "print \"%ComSpec%\""
C:\>perl -e "print '%ComSpec%'"
C:\>perl -e "print qq(%ComSpec%)"
C:\>perl -e "print qq(\%ComSpec\%)"
Microsoft(R) Windows DOS
(C)Copyright Microsoft Corp 1990-2001.

C:\>echo %ComSpec%

C:\>echo %COMSPEC%

C:\>set|findstr /i "COMMAND"

C:\>echo %COMSPEC%

Microsoft(R) Windows DOS
(C)Copyright Microsoft Corp 1990-2001.

C:\>echo %COMSPEC

C:\>perl -e "print '%COMSPEC%'"
C:\>perl -e "print \"%COMSPEC%\""
C:\>perl -e "print qq(\%COMSPEC\%)"

----- Original Message ----- 
From: "Tim Maher" <tim at consultix-inc.com>
To: <spug-list at pm.org>
Sent: Wednesday, March 23, 2005 10:33 AM
Subject: SPUG: One-liners on Windows via command.com

Can somebody give me pointers on how to modify Unix-ish one-liners
to work on Windows systems?

For example, this works on UNIX/Linux, because the single quotes (SQs)
are recognized as forming a literal string out of their contents,
which gets passed as an argument to the command:

perl -wle 'print "Crikey";'

On Win/XP, the same command elicits this Perl error:

C:\ perl -wle 'print "Crikey";'
Can't find string terminator "'" anywhere before EOF at -e line 1.

I had expected some indication that command.com wouldn't know what
to do with the SQ, as opposed to an indication that Perl only got
a single one in -e's argument!  Can somebody explain what SQs do in
this shell?

On the other hand, I find that DQs as exterior quotes work fine
(on XP), and the backslash even appears to quote nested DQs, as
it does in the Unix shells:

C:\ perl -wle "print \"Crikey\";"

and qq operator works:

C:\ perl -wle "print qq{Crikey};"

as does the arguably more appropriate q operator:

C:\ perl -wle "print q{Crikey};"

or the SQ itself:

C:\ perl -wle "print 'Crikey';"

So now I'm wondering, do DQs in this shell allow any interpolations?
If so, what Perl symbols would need to be quoted to suppress that?
Where does one get documentation on the workings of this shell?

"man perlport" provides lots of info on modifying Perl programs
/themselves/ for portability, but it has virtually nothing to say
on the subject of passing Perl code as a command-line argument.

Can somebody point me to their favorite tutorial covering the use
of command-line Perl techniques with Win32 systems?

| Tim Maher, PhD     (206) 781-UNIX      (866) DOC-PERL     (866) DOC-UNIX |
| tim(AT)Consultix-Inc.Com  http://TeachMePerl.Com  http://TeachMeUnix.Com |
|    Watch for my June, 2005 book: "Minimal Perl for UNIX/Linux People"    |

More information about the spug-list mailing list