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

This is one of the most newbish questions ever... How do I just make my script, say, print something then wait for the user to pass an arg, NOT just pass the arg when they give the perl command, and then store whatever they said in a variable?
  • Comment on Simple Q: Make it prompt for a variable

Replies are listed 'Best First'.
Re: Simple Q: Make it prompt for a variable
by Nkuvu (Priest) on Jul 11, 2004 at 19:21 UTC
    #!/usr/bin/perl use strict; print "Enter your name: "; my $name = <STDIN>; chomp $name; print "Hello, $name\n";
Re: Simple Q: Make it prompt for a variable
by Zaxo (Archbishop) on Jul 11, 2004 at 19:29 UTC

    If you have file names in the command line, <> will not do:

    my $prompt = 'Prompt: '; print $prompt; chomp( my $var = <STDIN> );

    After Compline,
    Zaxo

Re: Simple Q: Make it prompt for a variable
by pbeckingham (Parson) on Jul 11, 2004 at 19:19 UTC

    Try:

    print "Enter something: "; my $something = <>;

Re: Simple Q: Make it prompt for a variable
by BUU (Prior) on Jul 11, 2004 at 19:22 UTC
    Unless I'm missunderstanding you, you want to read from stdin. Do so with the standard readline function or operator: <>, like
    $|=1; #majick print "Please enter data:\n"; my $data = <STDIN>;

      What is the purpose of the unbuffering there?

        Actually ya know what, I'm not sure it has a point. I was just thinking about cases dealing with trying to read and print to stdout/in and some times having problems with the "prompt" not being printed until you read from stdin.
Re: Simple Q: Make it prompt for a variable
by roju (Friar) on Jul 12, 2004 at 15:33 UTC
    Look at Term::Readline. Automatically adds history, editing, etc. Makes programs that present a shell much nicer to deal with, IMHO. Also almost trivial to add support for.