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

my $argument = $ARGV[0] || die "No parameters given.\n";
I have an argument that gives me the message "No parameters given" if I dont put an argument it gives me the message above but how can I get it to prompt me for an argument if not entered at command line so it doesnt exit out of program each time I forget to put an argument at the command line???

Example:
scriptname #I enter my scriptname without an argument.

#output I would like to have if I forget to put in a parameter at command line.
You did not enter a parmeter, please enter a parameter:

Replies are listed 'Best First'.
Re: Command line argument
by vladb (Vicar) on Jun 03, 2002 at 13:11 UTC
    Do exactly what you've just said. If no parameter specified to your program, just request the user for input. And unless there's no input either, simply quit the program then ;)
    my $foo_arg = $ARGV[0]; # or just shift(); unless ($foo_arg) { print "Foo arg is missing! Please enter:\n"; $foo_arg = <STDIN>; chomp($foo_arg); die "Alright, i'm just quitting this one!" unless $foo_arg; }


    _____________________
    $"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}
      Thanks for all of your quick responses!!
      Now the script works.
Re: Command line argument
by derby (Abbot) on Jun 03, 2002 at 13:38 UTC
    Well my own $.02 would be - "don't do this." Either have command line switches (GetOpt::Std) or prompt. Please don't do both - the prompts will annoy those who want pure command line and switches will confuse those who prefer the prompts. Sure the script can do both but just not at the same invocation. Something along these lines would be ideal:

    if( @ARGV ) { process_cmdline(); } else { prompt(); }

    There's just something about mixing them that makes me go yech.

    -derby

Re: Command line argument
by tadman (Prior) on Jun 03, 2002 at 13:11 UTC
    The way you've stated your question is highly confusing, but to your credit, that first sentence is certainly long enough to warrant a full three question marks.

    Normally, when making quick shell programs, what I do is something like this:
    unless (@ARGV) { die "Usage: $0 list_of_files_to_mangle_and_destroy\n"; }
    Don't forget that zero is false, so if someone called your program on a file called "0", they are going to get an error.

    For something more sophisticated, you might want Getopt::Long.
Re: Command line argument
by Zaxo (Archbishop) on Jun 03, 2002 at 13:11 UTC

    This does what you want, but your users have to know about Ctrl-D:

    my $argument = $ARGV[0] || (print( "Parameter? "), (<>)[0]); chomp $argument;
    Note that the comma after print() is the sequence operator here.

    Update: chomped at ++tadman's suggestion. Also, this removes the Ctrl-D problem:

    my $argument = $ARGV[0] || (print( "Parameter? "), <>); chomp $argument;

    After Compline,
    Zaxo

Re: Command line argument
by Abigail-II (Bishop) on Jun 03, 2002 at 13:44 UTC
    There are two problems with the syntax you used above. First, the || is better written as or, although in this particular case, the effect is more or less the same. A worse problem is that your code will die if the program is given an argument that's false, for instance 0.

    I'd write something like:

    my $argument = @ARGV ? shift : do { print "Please give an argument: "; my $arg = <STDIN>; chomp $arg; $arg; };

    Abigail

Re: Command line argument
by krujos (Curate) on Jun 03, 2002 at 13:10 UTC
    you could just check for it before.
    #!/usr/bin/perl $argument = $ARVG[0] || warn "No argument"; print "argv = |$ARVG[0]|\n"; if ($argument !=~/\w/ ) { print "enter an argument \n"; chomp ($argument = <STDIN>); }

    .... or something like that. in the reg ex you could even check for the right type of input.

    update
    thanks to tadman for pointing out this, but I was missing an equals sign in the above.. also, got a cup of coffee in me and fixed other silly stuff.. sorry for the first poor response.
      Say what? That's the same as this:
      $argument = undef; while (0) { }
      This is the noise you'll be making when you try and run that program: "ARVG! ARVG!"

      Remember, when pushing buttons on the keyboard, it is important to hit them in the right order. Must be one of those late nights/early mornings?

      I think what you meant was:
      $|++; my $argument = $ARGV[0] || warn "No argument given\n"; do { print "Enter an argument: "; chomp ($argument = <STDIN>); } while (!length($argument));