Arguements @ Runtime


Read With Formatting | Free Open Source Tutorials Account

Perl Programming
Thread: Arguements @ Runtime


NandKishore
Dear All ,

I am a newbie to Perl world. I am trying to create a script get_details.pl which can take 3 different options at runtime -d <Number>, -m <Number>, -a. How do I handle them in my script.

That is user can run this script as follows.

get_details.pl -d <NUMBER>
get_details.pl -m <NUMBER>
get_details.pl -a

These are the only valid options available. Also these options are exclusive that is if the user is using the -d option then they cannot use the -m or -a option. What is the most effecient way of handling this kind of logic. Do I have to always use SHIFT or is there a effecient way out.

Thanks a lot.

Nand Kishore

md_doc
Perl arguments are stored in an array called @ARGV

If you want to access the first argument you would reference it as $ARGV[0]. The second argument would be $ARGV[1];

So you might want to do something like.


switch ($ARGV[0]) {
case "-a" { maybe call the function for a? }
case "-m" { maybe call the function for m? }
case " -d" { maybe call the function for d?}
else { print "valid arguments are -a or -m [number] or -d [number]\n" }
}


then when you call the -m and -d function just pass $ARGV[1] and make sure it is a number or else return an error there.

Just an idea but it should at least get you started in the right direction.