Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Pre-populating <STDIN> with a default value

by obonaventure (Acolyte)
on Dec 18, 2008 at 01:41 UTC ( [id://731126]=perlquestion: print w/replies, xml ) Need Help??

obonaventure has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a project where I will need to prompt the user for some information. I'd like the answer to be "pre-populated" on the screen and if the user would like they can backspace over it and type in a different answer or just hit enter as if they typed in the default value themself. I've played around with <STDIN> but I cannot make it work exactly as I'd like. I like the question to look like this:

What type of server?: BRANCH

With BRANCH being the default answer and if the user wants backspace over the word BRANCH and type in another answer or just hit enter. Does anyone have any ideas or other suggested methods to use?
  • Comment on Pre-populating <STDIN> with a default value

Replies are listed 'Best First'.
Re: Pre-populating <STDIN> with a default value
by graff (Chancellor) on Dec 18, 2008 at 02:24 UTC
    The more typical method for this sort of thing is that the prompting string shows the default value that would be used if the person just hits 'Enter' as a response -- this is how the CPAN shell works, for instance:
    Do you want to do the normal thing? [yes] : Do you want to do something strange, tedious, and/or risky? [no] : What you want as your return address? [you@there] : ...
    (In each case, none of the displayed text gets erased. This can actually be a nice feature, when someone decides they want to paste the dialog into a bug report or mail message, so the recipient can see what choices were offered as defaults, as well as the non-default answers, if any, that were given.)

    It's nice not having to delete stuff first in order to type in the value that you really want. The Term::Readline module suggested above, which you should use anyway, does make it easy -- ^U would clear the input line, for instance, as well as repeated use of the "Delete" key -- but the easiest thing is: type nothing but "Enter" to accept the default, and when you don't want the default, type only the value you want without further ado.

Re: Pre-populating <STDIN> with a default value
by Fletch (Bishop) on Dec 18, 2008 at 01:46 UTC
Re: Pre-populating <STDIN> with a default value
by JadeNB (Chaplain) on Dec 18, 2008 at 01:48 UTC
    Will IO::Prompt do what you need?

    UPDATE: And I woulda got away with it, too, if it hadn't been for Fletch!

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Pre-populating <STDIN> with a default value
by belg4mit (Prior) on Dec 24, 2008 at 01:58 UTC
    The conventional method is to simply show the default in brackets and use that if the response is empty. You could also probably work something bass-ackwards out with FileHandle::Unget

    --
    In Bob We Trust, All Others Bring Data.

Re: Pre-populating <STDIN> with a default value
by eff_i_g (Curate) on Dec 19, 2008 at 21:24 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://731126]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found