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

Hi Monks
I'm currently working on a program that retrieves sets of data. This is working quite well now but I have to run my program several times for each type of retrieval I have to do.
What I would like to be able to do is specify what types of retrival I would like to do as arguments passed in before the program starts. The problem is that I need my program to be able to handle any number of arguments i.e. from one to infinity.
My question is how could I input all the arguments and how would I get my program to know when the arguments have ended?

perl program name argument perl program name argument argument perl program name argument argument argument . . . etc

JohnIrl

Sum day soon I'Il lern how 2 spelI (r tYpe)

Replies are listed 'Best First'.
Re: Multiple Arguments
by moxliukas (Curate) on Jul 22, 2002 at 09:02 UTC

    Well, all the arguments are stored in @ARGV array, so you can get the length of it with

    scalar @ARGV;

    or alternatively, you can find the number of the last element with

    $#ARGV;

    I hope that helps...

Re: Multiple Arguments
by kodo (Hermit) on Jul 22, 2002 at 09:05 UTC
    Hmm not sure if I understood what you mean, but maybe all you want to do is to use @ARGV instead of $ARGV[0], $ARGV[1], etc?
    You could also do something like:

    while (@ARGV) { local $_ = shift(@ARGV); ... }

    giant
Re: Multiple Arguments
by flocto (Pilgrim) on Jul 22, 2002 at 09:34 UTC

    If you need more complex argument parsing then just shifting @ARGV (see above) you might want to take a look at Getopt::Long which provides POSIX compatible argument parsing.

    Regards,
    -octo

Re: Multiple Arguments
by Abigail-II (Bishop) on Jul 22, 2002 at 12:58 UTC
    All the passed arguments to your program are found in the (special) array @ARGV. And we have standard Perl idiom to iterate over an array, without the need to know the size of the array: foreach. So you'll get code like:
    foreach my $argument (@ARGV) { ... do something with $argument ... }
    Abigail
Re: Multiple Arguments
by dimmesdale (Friar) on Jul 22, 2002 at 14:01 UTC
    you may also want to search CPAN for GetOpt::Regex, depending on your needs (http://search.cpan.org). It allows you to handle command line args flexibly with regular expressions (regexes).