Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > newbie: specifying default text for STDIN keyboard input?

Reply
Thread Tools

newbie: specifying default text for STDIN keyboard input?

 
 
Mad Scientist Jr
Guest
Posts: n/a
 
      04-10-2006
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;

 
Reply With Quote
 
 
 
 
Mad Scientist Jr
Guest
Posts: n/a
 
      04-10-2006
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 $_;
}
}

################################################## #############################

 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      04-10-2006

"Mad Scientist Jr" <> wrote in message
news: oups.com...
>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


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-11-2006
Mad Scientist Jr <> wrote:

> $color=<STDIN>;
> chop($color);



Do not use chop() to remove line endings.

Use chomp() to remove line endings.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Mad Scientist Jr
Guest
Posts: n/a
 
      04-11-2006

>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 Garrish wrote:
> "Mad Scientist Jr" <> wrote in message
> news: oups.com...
> >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


 
Reply With Quote
 
Mad Scientist Jr
Guest
Posts: n/a
 
      04-17-2006
thanks... I'll give it a try

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

Marc Dashevsky wrote:
> Mad Scientist Jr <> writes in article %:
> >
> > >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);
> }
>
> --
> Go to http://MarcDashevsky.com to send me e-mail.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
peek at stdin, flush stdin Johnathan Doe C Programming 5 4 Weeks Ago 04:30 PM
How to pass stdin of a C++ program to the stdin of a process createdwith ShellExecute() Ben C Programming 2 08-29-2009 09:47 PM
STDIN, OUT, ERR and $stdin, out, err - Differences? Terry Cooper Ruby 7 06-09-2009 05:48 AM
Reading from stdin then launching a program that reads from stdin strange behaviour Stefano Sabatini Perl Misc 6 07-29-2007 10:38 PM
Reading stdin once confuses second stdin read Charlie Zender C Programming 6 06-21-2004 01:39 PM



Advertisments