
BillKSmith
Veteran
Mar 21, 2013, 8:05 PM
Post #2 of 3
(76 views)
|
|
Re: [mar85] How to Limit <STDIN> User-Input Entry?
[In reply to]
|
Can't Post
|
|
push @user_input, scalar <STDIN>; The second of argument of push is a LIST. (Refer perldoc -f push) This imposes list context on <STDIN>. In list context, the diamond operator (<>) reads an entire file. Your code actually works as you expect if you manually enter an end-of-file character after every input record. (Under windows, enter one datum, RETURN, and CTRL-Z) As a matter of style, always use strict and warnings. Seldom (if ever) use the C-style for loop. In this case, I think it is clearer to use a scalar variable to impose scalar context on <>. Use <> rather than the explicit <STDIN> unless you know the difference. Even better, use a prompt module from CPAN. The following code does not chomp the input becuse the returns are neded in the output.
use strict; use warnings; my @user_input; for my $i (1..5) { print "Enter entry no.$i: \n"; my $datum = <>; push @user_input, $datum; } print @user_input, "\n"; Good Luck, Bill
(This post was edited by BillKSmith on Mar 21, 2013, 8:06 PM)
|