newbie: specifying default text for STDIN keyboard input?

Discussion in 'Perl Misc' started by Mad Scientist Jr, Apr 10, 2006.

  1. 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;
     
    Mad Scientist Jr, Apr 10, 2006
    #1
    1. Advertising

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

    ###############################################################################
     
    Mad Scientist Jr, Apr 10, 2006
    #2
    1. Advertising

  3. Mad Scientist Jr

    Matt Garrish Guest

    "Mad Scientist Jr" <> wrote in message
    news:...
    >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
     
    Matt Garrish, Apr 10, 2006
    #3
  4. 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
     
    Tad McClellan, Apr 11, 2006
    #4

  5. >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:...
    > >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
     
    Mad Scientist Jr, Apr 11, 2006
    #5
  6. 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.
     
    Mad Scientist Jr, Apr 17, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wodny

    Return stdin to keyboard

    Wodny, May 12, 2005, in forum: C++
    Replies:
    2
    Views:
    434
    Wodny
    May 12, 2005
  2. herrcho
    Replies:
    23
    Views:
    1,353
  3. Replies:
    3
    Views:
    772
    Chris Dollin
    Aug 31, 2005
  4. Replies:
    4
    Views:
    685
    Walter Roberson
    Sep 9, 2005
  5. Peter Blues

    stdin (keyboard) to file

    Peter Blues, Feb 6, 2008, in forum: C Programming
    Replies:
    9
    Views:
    610
    CBFalconer
    Feb 6, 2008
Loading...

Share This Page