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

Hi everyone!

Probably it is not a perl question, but.. I can't understand how "^" symbol is handled in Windows command line here:

D:\TEMP>perl -e "print shift" q^w qw D:\TEMP>perl -e "print shift" q^^w q^w D:\TEMP>perl -e "print shift" q^^^w q^w

Under unix it always prints argument unchanged.

Also, the followng code perl -e "print $ARGV[0]" prints nothing in Windows, and prints ARRAY(0x80fbdcc) in linux..

--dda

Replies are listed 'Best First'.
Re: Windows command line
by BrowserUk (Patriarch) on Oct 18, 2003 at 16:10 UTC

    '^' acts an escape character on the CMD command line, a little like '\' under unix, but much more limited. The only characters that it escapes are a few characters that the CLI otherwise treats specially.

    These are  | < > % ^

    Used, outside of double quotes, it prevents the command line from treating these characters as pipes, io-redirection or the start of an environment variable substitution respectively, as well as escaping itself.

    A simple demonstration.

    P:\test>perl -e"print qq[($_)] for @ARGV" ^| ^< ^> ^% ^^ (|)(<)(>)(%)(^)

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!

      Also like '\', it can be used as a line-continuation character.
      C:\>echo hi the^ More? re hi there

      My understanding is that this behaviour is undocumented. Did you find this out by experimentation or did you manage to unearth something from MSN that nobody else I know of has found?

      Incidentally, I have a patch for the README.win32 (which is automatically converted to perlwin32.pod when perl is built) that expands on the explanation of quoting rules (that I figured out by experimentation), perhaps you and I should knock together a better patch and get it included? The doc currently mentions ^ but doesn't explain it as well as you did (IMO).


      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi


        Essentially, it is the result of experimentation and as such subject to the whims of change by MS. It has been consistant (if ecclectic) for a good while now though.

        If you want to add that to your patch I'm all for it.

        The harder one is working out the treatment of double quotes...

        P:\test>perl -le"print qq[($_)] for @ARGV" "fred"""" """""bill" (fred") ("bill) P:\test>perl -le"print qq[($_)] for @ARGV" "fred" "bill" (fred) (bill) P:\test>perl -le"print qq[($_)] for @ARGV" "fred"" "bill" (fred") (bill) P:\test>perl -le"print qq[($_)] for @ARGV" "fred"" ""bill" (fred") (bill) P:\test>perl -le"print qq[($_)] for @ARGV" "fred"" """bill" (fred") ("bill) P:\test>perl -le"print qq[($_)] for @ARGV" "fred""" """bill" (fred" "bill) P:\test>perl -le"print qq[($_)] for @ARGV" "fred""a" ""a"bill" (fred"a "abill) P:\test>perl -le"print qq[($_)] for @ARGV" "fred"a"" "a""bill" (freda) (a"bill)

        If you can discern a pattern in that lot, I've several hundred more weird examples for analysis:)


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        Hooray!

Re: Windows command line
by pg (Canon) on Oct 18, 2003 at 15:44 UTC
    (my $copy = $ARGV[0]) =~ s/([^[:print:]])/sprintf " (0x%02x) ", ord $1 +/ge; print $copy;

    The above piece of code can help you figure out windows' interpretation of what you keyed. It prints all the non-printable chars as hex.

    For example, if you punch "Ctrl-^" "w", which appears on your screen as "^^w" (I guess that's how you get it), this program tells you window takes that "Ctrl-^" as 0x1e.

    Update:

    At one point, I had the same thought as BrowserUK, that ^ is an escape char, but after I tested on my PC, it does not behave like that... so not all versions. (well I am still running win98)

      Indeed. I should have emphasised the CMD.EXE.

      Win95/98 (I'm not sure about ME) all use COMMAND.COM as their CLI, which has a completely different set of (equally unwritten:) rules.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      Hooray!

      Thanks, pg. But I didn't "key" or "punch" anything. I just typed "^" (shift-6).

      I'll investigate this some more.

      --dda