PDA

View Full Version : Commandline arguments in Perl



absolutezero1287
November 5th, 2009, 07:36 AM
So how do I do it?

I wrote a script that has many subroutines for doing various mathematical operations (solve quadratics, find the sum of all numbers in a finite series, midpoint between two lines, and others). I named the script math.pl but what I'd like it to do is print a usage message if no argument is passed, e.g. USAGE -g run the gauss subroutine, etc. etc. I figured that I could do something like:

if (@ARGV = "") {
print "USAGE:\n-g run the gauss subroutine\n";
}

However this doesn't look right.

diesch
November 5th, 2009, 08:26 AM
if (not @ARGV) {
print "USAGE:\n-g run the gauss subroutine\n";
}

absolutezero1287
November 5th, 2009, 08:35 AM
if (not @ARGV) {
print "USAGE:\n-g run the gauss subroutine\n";
}

Ok, I understand that bit but how would I get it to recognize an argument such as -g, --help, --version, and others.

diesch
November 5th, 2009, 08:54 AM
Have a look at at the Getopt::Long module