Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

STDIN & STDOUT on Win32 ?

by tomazos (Deacon)
on Mar 01, 2006 at 11:13 UTC ( [id://533618]=perlquestion: print w/replies, xml ) Need Help??

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

I feel a bit silly...

#!perl # pipetest.pl use strict; use warnings; while (<STDIN>) { print "$_\n"; }

Now on a Win32 cmd.exe...

C:/> ./pipetest.pl < somefile C:/>

Prompt returns. Nothing happens. There is definately data in somefile. I am clearly missing something very obvious. ?

When I run it without taking stdin from a file:

C:/> ./pipetest.pl fsanlfga fsanlfga

It works fine and echos the lines I type.

Any ideas?

-Andrew.

Replies are listed 'Best First'.
Re: STDIN & STDOUT on Win32 ?
by BrowserUk (Patriarch) on Mar 01, 2006 at 12:13 UTC

    If you have AS perl and/or their htmlized docset, then this is mentioned under the heading STDIN and STDOUT, and Piping don't always work on NT..

    Although for my taste it doesn't explain the cause well or offer all the solutions.

    If you want your program to be able to accept input from the command line or from a file specified on the command line, is to use the 'diamond operator' <> instead of <STDIN>:

    C:\test>type junk1.pl #! perl -slw use strict; print "?$_?" for <>; C:\test>junk1 hello world ^Z ?hello world ? C:\test>junk1 junk1.pl ?#! perl -slw ? ?use strict; ? ?print "?$_?" for <>; ?

    However, that won't work for piping like this:

    C:\test>type junk1.pl|junk1 The process tried to write to a nonexistent pipe.

    Instead you have to use

    C:\test>type junk1.pl | perl junk1.pl ?#! perl -slw ? ?use strict; ? ?print "?$_?" for <>; ?

    or use the AS solution, pl2bat(.bat) which can be found in x:\yourperl\bin, (assuming you have AS perl :)

    What that does, is wrap yourscript.pl in some batch code and outputs it as yourscript.bat, which then allows you to use both

    yourscript <file and

    type file | yourscript syntaxes without decoration:

    C:\test>pl2bat junk1.pl C:\test>type junk1.bat @rem = '--*-Perl-*-- @echo off if "%OS%" == "Windows_NT" goto WinNT perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl :WinNT perl -x -S %0 %* if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl if %errorlevel% == 9009 echo You do not have Perl in your PATH. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul goto endofperl @rem '; #! perl -slw #line 15 use strict; print "?$_?" for <>; __END__ :endofperl C:\test>junk1 <junk1.pl ?#! perl -slw ? ?use strict; ? ?print "?$_?" for <>; ? C:\test>type junk1.pl | junk1 ?#! perl -slw ? ?use strict; ? ?print "?$_?" for <>; ?

    The downside of this is that if you edit yourscript.pl the changes do not get reflected into yourscript.bat unless you re-run the pl2bat. Which will give you a moment or twos pause for thought if you've forgotten that you converted a script to a .bat and you find that your edits to it don't seem to make any difference!


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: STDIN & STDOUT on Win32 ?
by Corion (Patriarch) on Mar 01, 2006 at 11:44 UTC

    "Invoking" a Perl file on Win32 via the shell filetype connection does not pass any parameters, and it also doesn't seem to pass the redirected STDIN handle. You want perl -w ./pipetest.pl <somefile, which will work regardless of what quirks the Windows shell has there.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-28 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found