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

Hello Monks!
Let's say I want to input a string from the user. Something like:

print "Please enter a string: ";
chomp ($string_entered=<STDIN>);

Now, in the case I want to give the user a default string, like say "Hello username";
What the user could do is just hit enter in which case the string would be "Hello username" or else hit the backspace, and enter his name in case of the username. Also, he could take of "Hello username" entirely and enter another string, like "Bye Bye script".

All the above actions should take place in the command line, something like an interactive interface.

c:\somescript.pl
Please enter a string
Hello usernam_

Any ideas?

Thanks!

SRK.

Replies are listed 'Best First'.
Re: Input from the User
by ctilmes (Vicar) on Oct 21, 2003 at 13:04 UTC
    Take a look at Term::ReadLine:
    use warnings; use strict; use Term::ReadLine; my $term = Term::ReadLine->new(); my $string_entered = $term->readline('Please enter a string: ', 'Hello username'); print "You entered [$string_entered]\n";
Re: Input from the User
by delirium (Chaplain) on Oct 21, 2003 at 13:06 UTC
    There are security issues when you take user input and don't validate it, which the following code boldly ignores. Here is how I approached the same problem with some old code I used for an install package:

    sub getdir { my $valid=0; my $answer; my ($type,$default)=@_; my $blurb='Directory to install '; while(!$valid) { print "$blurb$type [$default]\n : "; chomp($answer=<STDIN>); $answer=$answer || $default; chop $answer if $answer =~ m#/$#; if(chdir $answer){$valid=1;} else{print "Can't access directory \"$answer\". Try again\n";} } return $answer; } my $docroot=getdir('HTML files','/var/www'); my $scripts=getdir('CGI scripts','/usr/lib/cgi-bin');

Re: Input from the User
by ptkdb (Monk) on Oct 21, 2003 at 15:09 UTC
    Quick way of doing it with PerlTk:
    use strict ; use Tk ; my($mw) = new MainWindow(-title => "Enter Username:") ; my($str) = "usernam_" ; my($endSub) = sub { destroy $mw } ; my($entry) = $mw->Entry(-textvariable => \$str)->pack(-side => 'left', + -fill => 'x', -expand => 1) ; $mw->Button(-text => "Ok", -command => $endSub)->pack(-side => 'left') + ; $mw->Button(-text => "Cancel", -command => sub { exit })->pack(-side = +> 'left') ; $entry->focus ; $entry->icursor('end') ; $entry->bind('<Return>', $endSub) ; MainLoop ; print "result = $str\n" ;
    User can press 'Enter' or the OkButton.
Re: Input from the User
by batkins (Chaplain) on Oct 21, 2003 at 13:10 UTC
    I'm not really sure what you're asking. You've already got most of the code you need. If you want to do some kind of default thing, do this:
    $entered_string = "default_text" if $default_text eq "";
    This looks suspiciously like homework....

    The computer can't tell you the emotional story. It can give you the exact mathematical design, but what's missing is the eyebrows. - Frank Zappa
Re: Input from the User
by Abigail-II (Bishop) on Oct 21, 2003 at 14:27 UTC
    I usually do something like:
    print "Bla, bla, bla "; # Well, this is what I tend to leave off. chomp (my $answer = <>); $answer = $default_value unless $answer =~ /\S/;

    Abigail

Re: Input from the User
by Grygonos (Chaplain) on Oct 21, 2003 at 13:06 UTC

    in a very basic way this is what you want to accomplish...I guess you are asking for a name? And since the 'hello' portion is imputed upon the string...and the user isn't actually going to enter 'hello me' as the string, rather they would enter their name simply 'me', there is no way to account for someone entering 'bye bye soandso'. The snippet below accounts for the user entering nothing and then imputing a default string upon the user.

    #!/perl -w use strict; print "enter user name: "; chomp(my $username =<STDIN>); if($username) {print "hello ". $username ."\n";} else {print "hello default user\n";}
    I think I totally misunderstood the question... my apologies.