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 $_;
}
}
###############################################################################