user152037
user152037

Reputation: 209

Suggest a value when asking for <STDIN>

I want the user to just press enter for the perl script to continue. Example:

# in the line below the 'y' should be given as a suggestion, 
# thus only pressing "enter" to continue.

print "Are you over 18? (y/n): "; 
chomp(my $allow = <STDIN>);

Upvotes: 2

Views: 82

Answers (2)

Matthew Franglen
Matthew Franglen

Reputation: 4532

You can print the carriage return character without a linefeed. This moves the caret to the beginning of the line which then makes any subsequent text overwrite it:

print "Are you over 18? (y/n):\ny\r";
chomp(my $allow = <STDIN>);
$allow ||= "y"; # Accept default if nothing supplied

Upvotes: 2

mpapec
mpapec

Reputation: 50657

print "Are you over 18? (Y/n): "; 
chomp(my $allow = <STDIN>);
$allow ||= "y";

if (lc($allow) ne "y") {
  die "you're not allowed\n";
}

Upvotes: 3

Related Questions