newbie: specifying default text for STDIN keyboard input?

M

Mad Scientist Jr

I want to prompt a user to type some input, and have the prompt display
a default value. Is this possible and if so how? Thanks...

#!/usr/local/bin/perl -- -*-perl-*-
use strict;
use warnings;
my $color = '';
print "What color do you prefer? ";
$color=<STDIN>;
#Q: how do you specify a default value to be inserted at the prompt, eg
"green"?
chop($color);
print "You chose ".$color;
 
M

Mad Scientist Jr

I found a couple functions on the web that sort of do this (though they
don't insert the text at the prompt line):

#!/usr/local/bin/perl -- -*-perl-*-
use strict;
use warnings;
use Text::Wrap;

#DIM VARIABLES
my $color = '';

#GET INPUT
print "What color do you prefer? ";
$color=<STDIN>;
print "you chose ".$color."\n";

$color = &promptUser("What color do you prefer? ", "green");
print "you chose ".$color."\n";

$color = &AskUser("What color do you prefer? ", "blue");
print "you chose ".$color."\n";

###############################################################################
# VERSION 2
sub AskUser
{
my ($question, $default) = @_;

my $answer;
if (InInteractiveMode())
{
$question .= " [$default]" if (defined $default);
PromptUser2($question);
chomp($answer = <STDIN>);
}

$answer = $default unless (defined $answer and length $answer);

return $answer;
}

sub PromptUser2
{
print(wrap('', '', shift), "\n") if InInteractiveMode();
}

sub InInteractiveMode { return 1 if (-t STDIN and -t STDOUT) }

###############################################################################
# VERSION 1

#----------------------------( promptUser
)-----------------------------#
#
#
# FUNCTION: promptUser
#
#
#
# PURPOSE: Prompt the user for some type of input, and return the #
# input back to the calling program. #
#
#
# ARGS: $promptString - what you want to prompt the user with #
# $defaultValue - (optional) a default value for the prompt #
#
#
#-------------------------------------------------------------------------#

sub promptUser {


#-------------------------------------------------------------------#
# two possible input arguments - $promptString, and $defaultValue
#
# make the input arguments local variables.
#

#-------------------------------------------------------------------#

my ($promptString,$defaultValue) = @_;


#-------------------------------------------------------------------#
# if there is a default value, use the first print statement; if
#
# no default is provided, print the second string.
#

#-------------------------------------------------------------------#

if ($defaultValue) {
print $promptString, "[", $defaultValue, "]: ";
} else {
print $promptString, ": ";
}

$| = 1; # force a flush after our print
$_ = <STDIN>; # get the input from STDIN (presumably the
keyboard)


#------------------------------------------------------------------#
# remove the newline character from the end of the input the user #
# gave us. #
#------------------------------------------------------------------#

chomp;

#-----------------------------------------------------------------#
# if we had a $default value, and the user gave us input, then #
# return the input; if we had a default, and they gave us no #
# no input, return the $defaultValue. #
# #
# if we did not have a default value, then just return whatever #
# the user gave us. if they just hit the <enter> key, #
# the calling routine will have to deal with that. #
#-----------------------------------------------------------------#

if ("$defaultValue") {
return $_ ? $_ : $defaultValue; # return $_ if it has a value
} else {
return $_;
}
}

###############################################################################
 
M

Matt Garrish

Mad Scientist Jr said:
I want to prompt a user to type some input, and have the prompt display
a default value. Is this possible and if so how? Thanks...

#!/usr/local/bin/perl -- -*-perl-*-
use strict;
use warnings;
my $color = '';
print "What color do you prefer? ";
$color=<STDIN>;
#Q: how do you specify a default value to be inserted at the prompt, eg
"green"?
chop($color);
print "You chose ".$color;

Just assign a value if none is entered, or is there something I'm missing
here?

use strict;
use warnings;

print "What color do you prefer? [green] ";

my $color=<STDIN>;

chomp $color;

$color ||= 'green';

print "You chose ".$color;


Matt
 
M

Mad Scientist Jr

Just assign a value if none is entered, or is there something I'm missing here?

Rather than simply print a prompt with the default in brackets like:

<PRE>
Type a color [green]? _
^
cursor is after prompt
</PRE>

I would like the cursor to be IN FRONT of the default value (like it
would in a GUI):

<PRE>
Type a color? green
^
cursor is here, BEFORE default value
</PRE>


Matt said:
Mad Scientist Jr said:
I want to prompt a user to type some input, and have the prompt display
a default value. Is this possible and if so how? Thanks...

#!/usr/local/bin/perl -- -*-perl-*-
use strict;
use warnings;
my $color = '';
print "What color do you prefer? ";
$color=<STDIN>;
#Q: how do you specify a default value to be inserted at the prompt, eg
"green"?
chop($color);
print "You chose ".$color;

Just assign a value if none is entered, or is there something I'm missing
here?

use strict;
use warnings;

print "What color do you prefer? [green] ";

my $color=<STDIN>;

chomp $color;

$color ||= 'green';

print "You chose ".$color;


Matt
 
M

Mad Scientist Jr

thanks... I'll give it a try

ps understood about PERL not being a GUI. I'm suprised is even
possible...

Marc said:
Mad Scientist Jr said:
Just assign a value if none is entered, or is there something I'm missing here?

Rather than simply print a prompt with the default in brackets like:

<PRE>
Type a color [green]? _
^
cursor is after prompt
</PRE>

I would like the cursor to be IN FRONT of the default value (like it
would in a GUI):

The following does what you want, but it does not erase the default
value from the screen as soon as you type the first keystroke.
After all it's not a GUI.

use strict;
use warnings;

print GetInput('Enter your response', 'This is the default');

sub GetInput {
my($prompt, $default) = @_;
print "$prompt: $default";
print "\010" x length($default);
my $response = <STDIN>;
chomp($response);
return($response || $default);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top