Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

piping commands to perl via standard input on the Win32 command line

by frag (Hermit)
on Jan 09, 2002 at 22:31 UTC ( [id://137510]=perlquestion: print w/replies, xml ) Need Help??

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

According to the opening part of perlrun, this should work on the command line:
echo "print qq(Hello.\n)" | perl
That is, that should be equivalent to the plain ol' perl -e "print qq(Hello.\n)". And it does work, on Linux and on Win32 via Cygwin's bash shell. But it doesn't seem to work with the standard Win32 cmd.exe shell. This wouldn't be too surprising, except that I'm surprised that I'm not finding this documented anywhere (Camel, perldocs, here, Activestate, etc.). The closest I can find is this, which implies that piping is only a problem when running files that have associations.

Am I doing something wrong, or overlooking some documentation? And is this simply not going to work with cmd.exe?

(Incidentally, the motivation for this is to be able to use find2perl on Win32.)

-- Frag.
--
"Just remember what ol' Jack Burton does when the earth quakes, the poison arrows fall from the sky, and the pillars of Heaven shake. Yeah, Jack Burton just looks that big old storm right in the eye and says, "Give me your best shot. I can take it."

Replies are listed 'Best First'.
(tye)Re: piping commands to perl via standard input on the Win32 command line
by tye (Sage) on Jan 09, 2002 at 22:57 UTC

    The problem is that Win32's "echo" preserves the quotes and so is poorly suited for this task:

    C:\>echo "print qq(Hello.\n)" "print qq(Hello.\n)"
    vs.
    $ echo "print qq(Hello.\n)" print qq(Hello.\n)
    so you get the equivalent of perl -e "\"print qq(Hello.\n)\"" instead of perl -e "print qq(Hello.\n)" So piping other things to perl works fine in Win32 (so find2perl shouldn't be a problem -- I used it quite recently).

            - tye (but my friends call me "Tye")

      The problem is that Win32's "echo" preserves the quotes and so is poorly suited for this task- tye

      That's why you shouldn't echo quotes unless you mean for perl to see them as for example, this in CMD:

      echo system("time /T") ; print "\nHello world!\n" | perl -w

      10:47a
      Hello world!
      mkmcconn
      fiddled with example

        "echo" still isn't well suited for this. Many (even "most") Perl scripts will contain characters that Win32 command prompts won't give to "echo" (unless you put them in quotes which "echo" is too stupid to strip). In particular, < > | & ^ will cause problems while % might or might not be left alone.

                - tye (but my friends call me "Tye")
Re: piping commands to perl via standard input on the Win32 command line
by dmmiller2k (Chaplain) on Jan 09, 2002 at 23:08 UTC

    Generally, when the STDOUT of a Win/DOS command misbehaves (from my UNIX-y expectations), especially where CMD.EXE's builtin commands are involved (e.g., echo), I have found that prefixing the command with CMD /c often does the trick.

    That is, instead of:

    echo "print qq(Hello.\n)" | perl

    try this:

    cmd /c echo "print qq(Hello.\n)" | perl

    I use this primarily for capturing output from .BAT and .CMD files, but the builtin commands don't always work right either.

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime
      Actually, cmd /c echo "print qq(Hello.\n)" | perl doesn't work, either. (Although I didn't know about the /c switch before, so thanks for pointing that out.) As tye says, it's a fundamental problem with echo.

      -- Frag.
      --
      "Just remember what ol' Jack Burton does when the earth quakes, the poison arrows fall from the sky, and the pillars of Heaven shake. Yeah, Jack Burton just looks that big old storm right in the eye and says, "Give me your best shot. I can take it."

Re: piping commands to perl via standard input on the Win32 command line
by webengr (Pilgrim) on Jan 09, 2002 at 23:42 UTC
    Out of sheer curiosity, I tried this (WinNT4):
    C:>echo print qq/Hello world!\n/ | perl Hello world! C:>echo print qq/Hello world!\n/ print qq/Hello world!\n/
    So apparently the DOS echo command can/should be used without quotes, and perhaps uses the next recognized DOS operator (in this case a pipe) as a delimiter?

    PCS
Re: piping commands to perl via standard input on the Win32 command line
by thunders (Priest) on Jan 09, 2002 at 23:38 UTC
    omitting the quotes worked for me.
    C:\users\default>echo print qq(Hello.\n) |perl Hello.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://137510]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-24 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found