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

Hi Monks!
I am supposed to write a script that will take some arguments from the user via the command line, like this:
perl MY_SCRIPT.pl -p <program to run> -f <file1> -a <file2> -d <parame +ters file>
My question is if there is a sophisticated and safe way to for my script to be able to recognize if the user has specified an argument in the -p , the -f, the -a or the -d options that are available.
I know that $ARGV[0] is the 1st argument passed, $ARGV1 the 2nd etc, but I somehow need to "bind" the argument with the respective parameter.
For example, the -p parameter accepts a number (0,1,2) while all others a file.

Replies are listed 'Best First'.
Re: How to read the arguments in command line?
by GrandFather (Saint) on Jul 22, 2012 at 22:19 UTC
Re: How to read the arguments in command line?
by tobyink (Canon) on Jul 22, 2012 at 22:20 UTC

    If it's a small script, use Getopt::Long or Getopt::Long::Descriptive.

    If it's bigger, and all-singing, all-dancing, then take a look at App::Cmd.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Thanks guys, problem solved! I didn't know there was such a helpful function!