Thanks all for your inputs. I did not understand meaning of this line :( below. I only understood that it will exit while loop if the input has either yes or no. But then what is " grep { lc $normal eq $_ } "?
last if grep { lc $normal eq $_ } 'yes', 'no'; Thanks, Paryushan -----Original Message----- From: Gunnar Hjalmarsson [mailto:nore...@gunnar.cc] Sent: Saturday, February 28, 2009 3:35 AM To: beginners@perl.org Subject: Re: Acepting default value for user input Chas. Owens wrote: > On Fri, Feb 27, 2009 at 11:07, Gunnar Hjalmarsson <nore...@gunnar.cc> wrote: >> Sarsamkar, Paryushan wrote: >>> I would like to accept some user inputs (using <STDIN>), but it might be >>> easier for a user if I can provide the default value, so that they just >>> have to press ENTER. >>> >>> How can I do that? I've played around with <STDIN> but I cannot make it >>> work exactly as I'd like. >>> >>> E.g. >>> Do you want to do the normal thing? [yes] : >> >> print 'Do you want to do the normal thing? [yes] : '; >> chomp( my $normal = <STDIN> ); >> $normal ||= 'yes'; > snip > > This only works if 0 (or any false value other than '') is not a valid > response. I agree; should better be: $normal = 'yes' if $normal eq ''; Thanks for pointing it out. Which takes us to the obvious step of validating the user input. my $normal; while (1) { print 'Do you want to do the normal thing? [yes] : '; chomp( $normal = <STDIN> ); $normal = 'yes' if $normal eq ''; last if grep { lc $normal eq $_ } 'yes', 'no'; print "\nERROR: Answer must be yes or no.\n\n"; } -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/