GhodMode has asked for the wisdom of the Perl Monks concerning the following question:
Why does perl take a file name provided on the command line and open it for use with standard input? How should I prevent that?
I'm working on a program that takes a couple of files as command line arguments. At some point in the program, I want to prompt the user for an action to take.
The lines that accept the user's response look something like this:
print "Remove '$origin'? ([Y]es, [N], A(ll), ne[V]er; default: N) "; my $answer = <>; print "answer: |$answer|\n" if (DEBUG); chomp($answer); $answer = lc($answer); if ($answer eq ('y' or 'yes')) { unlink $origin; }
The problem is that <> is filled with the content of the first file on the command line. This is entirely unexpected. Why would the file be opened at all? I would have expected that filename to be treated only as a scalar value.
I confirmed this by adding while (<>) { print; } before I prompted for an answer.
To double-check, and eliminate problems caused by modules or other code, I put that line into a separate script and got the same results:
Thank you.#!/usr/bin/perl -w use strict; while (<>) { print; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: unexpected STDIN
by Corion (Patriarch) on Apr 30, 2011 at 06:17 UTC | |
by GhodMode (Pilgrim) on Apr 30, 2011 at 07:22 UTC | |
|
Re: unexpected STDIN
by CountZero (Bishop) on Apr 30, 2011 at 07:11 UTC | |
by GhodMode (Pilgrim) on Apr 30, 2011 at 12:06 UTC | |
by cdarke (Prior) on Apr 30, 2011 at 17:55 UTC | |
by CountZero (Bishop) on May 01, 2011 at 08:06 UTC | |
|
Re: unexpected STDIN
by wind (Priest) on Apr 30, 2011 at 06:48 UTC | |
by GhodMode (Pilgrim) on Apr 30, 2011 at 11:17 UTC | |
by AnomalousMonk (Archbishop) on Apr 30, 2011 at 14:13 UTC | |
by GhodMode (Pilgrim) on Apr 30, 2011 at 14:28 UTC | |
by locked_user sundialsvc4 (Abbot) on Apr 30, 2011 at 12:19 UTC |