Hi,

I'm trying to get a script to read a list of files from the command line or accept a list from STDIN (e.g. ls -1 | script.pl):

if (@ARGV){ foreach my $path(@ARGV){ $path=File::Spec->rel2abs($path); } $passhash{filearrayref}=\@ARGV; } elsif ($passhash{stdin}){ # flag to indicate input from stdin my $i=0; foreach my $path(<>){ chomp($path); $path=File::Spec->rel2abs($path); $passhash{filearrayref}[$i]=$path; $i++; } } else {die ("No input files specified.\a\n");}

The script subsequently asks for further input from STDIN (in a separate, unrelated subroutine):

print STDERR ("Directory \'$$pref{changedir}\' aleady exists. Overwrit +e?\n". "[Y(es)/N(o)/R(eselect destination)/C(lobber all duplicates)]". "\a\n"); my $answer = <STDIN>; chomp ($answer); # line 106 (see below) print "$answer\n"; # for testing purposes
The problem is that the second input gets ignored. The script doesn't wait for user input at all. In fact it behaves as though the variable $answer is never initialised. I'm using strict and warnings, and it tells me:
Use of uninitialized value in scalar chomp at script.pl line 106.

This problem doesn't occur if the flag variable $passhash{stdin} is not set. It looks suspiciously like the problem Sojourner described here, but the solution in that case (use CGI.pm) doesn't seem to apply here (this thing's going nowhere near a web site).

I'm sure I'm missing something simple here, but I can't for the life of me figure out what it is. Any help would be greatly appreciated.

Thanks.


In reply to STDIN following on? by tfrayner

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.