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

I am writing a small command line utility to allow users to change a stored value. So far I have written something like this:
my $record = { name => 'joe bloogs', age => 34, hobby => 'chess', }; for my $property ( keys %$record ) { print STDOUT "$property: "; chomp( $response = <STDIN> ); $record->{$property} = $response; }
Most of the time the user will leave the value unchanged or make minor spelling or capitalisation changes. To make it easier for them I’d like to push the existing value into the keyboard buffer so that it appears on the line as if they had just typed it. That way they can quickly amend it or just press return and it will come in through STDIN as if they had just typed it. Any help will be gratefully appreciated.

Replies are listed 'Best First'.
Re: adding default text to STDIN
by Fletch (Bishop) on Nov 09, 2005 at 16:14 UTC

    Look at something like Term::ReadLine. Or just print the current value in the prompt (print "$property [$record->{$property}]: ") and if $response eq '' ignore it (of course this prevents the user from actually providing a value of "nothing", but for most applications it's probably fine).

      Thanks a lot for your help. I used Term::ReadLine and got it working. I found the documentation a little lacking and it took me a little while to get it to work, so for everyone's future reference here is how I got it to work:
      use Term::ReadLine; my $record = { name => ‘joe bloogs’, age => 34, hobby => ‘chess’, }; my $term-> new Term::ReadLine 'user input'; for my $property ( keys %$record ) { my $response = $term->readline($property,$record->{$property}); $record->{$property} = $response; }
      I know that looks - and is - simple but it took a while for me to find the right call. Thanks again for your help
Re: adding default text to STDIN
by tinita (Parson) on Nov 09, 2005 at 16:31 UTC
    try:
    use ExtUtils::MakeMaker qw(prompt); $record->{$property} = prompt($property, $record->{$property});
    see perldoc ExtUtils::MakeMaker
      Thanks for your help. I saw fletch's answer first and went down that road, but I appriciate your answer.