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

Hello

My script entries need some arguments whose number and place are well-defined and need some arguments whose number is variable

For example, here is the beginning of my code. I am not sure it is correct.

print STDOUT "enter the name of the beacon\n"; my $beacon = $ARGV[0]; print STDOUT "enter the value of the begin_time\n"; my $begin_time = $ARGV[1]; print STDOUT "enter the value of the end_time\n"; my $end_time = $ARGV[2]; print STDOUT "enter the days\n"; my @DAYS = @ARGV;

Won't @ARGV overwrite the first $ARGV ? @ARGV can contain several days, but their number varies

Thanks a lot

Replies are listed 'Best First'.
Re: @ARGV: two different types of arguments
by GrandFather (Saint) on Jan 21, 2008 at 09:46 UTC

    You are confusing two different modes of operation - parsing command line arguments and prompting for user input then processing it.

    @ARGV is initialized with the arguments passed on the command line when your script is executed. You do not need to prompt for the values - it is too late for the user to change the values in any case.

    The prompt/process mode uses STDOUT (in exactly the way your code demonstrates) to prompt the user for input at run time. Your code should then use <STDIN> to get a line of response from the user. For example you could write:

    print STDOUT "enter the name of the beacon\n"; my $beacon = <STDIN>; ...

    You may then want to chomp $beacon to remove the trailing line end sequence and possibly validate the user provided value in some fashion.


    Perl is environmentally friendly - it saves trees
Re: @ARGV: two different types of arguments
by moritz (Cardinal) on Jan 21, 2008 at 09:36 UTC
    You shouldn't prompt the user for value he/she doesn't need to enter.

    You probably meant to do something like this:

    my $beacon = shift @ARGV; my $begin_time = shift @ARGV; my $end_time = shift @ARGV; my @DAYS = @ARGV;

    This will remove the first three elements of @ARGV and assigns the rest to @DAYS.

    See shift for details.

Re: @ARGV: two different types of arguments
by apl (Monsignor) on Jan 21, 2008 at 12:35 UTC
    I'd strongly suggest that you take a look at Getopt::Long for all your parameter-processing needs.