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

Hi!

I'm new here, although I've been playing with Perl for a number of years.

I'm having trouble redirecting an input file to STDIN for a commandline script. Here's an example of what I type in commandline:

test.pl < myfile.txt

Instead of processing the text file, the script just terminates as if the file was empty or the STDIN handle closed. It doesn't even process a single line in the text file.

I also tried the example at http://perlmonks.org/?node_id=191787 and get the same problem.

Any ideas on how I can get this working? I'm running Windows XP SP2 with ActivePerl 5.8.8 build 820.

Thanks!

Replies are listed 'Best First'.
Re: Redirecting STDIN on Windows commandline
by blazar (Canon) on Jul 04, 2007 at 21:13 UTC
    Instead of processing the text file, the script just terminates as if the file was empty or the STDIN handle closed. It doesn't even process a single line in the text file.

    Yes, it is a known problem, and somewhat of a pita. But if you use *ARGV's magic, a.k.a. the diamond operator <> you can safely specify the file on the command line just fine, and this is generally all you need:

    C:\temp>cat test.pl #!/usr/bin/perl -0777 use strict; use warnings; print scalar <>; __END__ C:\temp>test.pl test.pl #!/usr/bin/perl -0777 use strict; use warnings; print scalar <>; __END__
    I also tried the example at http://perlmonks.org/?node_id=191787 and get the same problem.

    Please use [id://191787] to render an internal link like Re: Re: Re: redirecting stdin on windows.

Re: Redirecting STDIN on Windows commandline
by BrowserUk (Patriarch) on Jul 04, 2007 at 18:25 UTC

    Use perl test.pl < file.txt instead.

    Or consider using pl2bat.bat to convert the perl script to a batch script. See the documentation for pl2bat.bat that is part of the AS docset for the full explanation and alternatives.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Redirecting STDIN on Windows commandline
by jbert (Priest) on Jul 04, 2007 at 17:07 UTC
    Did you try exactly the same script and test data as that link?

    One gotcha is that Windows treats a control-Z character as an end-of-file, so if your input file contains one of those you'll see nothing after it.

    You might also be hitting line-ending problems - was your datafile created (from scratch) in a windows editor such as notepad, or might it have unix line endings?