Command-Line Arguments :: @ARGV
* So how do we get command line arguments? Perl automatically stores any command line arguments into a global list called @ARGV.
* Each element in @ARGV sequentially contains the command-line arguments called with the program. You can parse these manually by hand, or use the more "sophisticated" getopt method below.
Command-Line Arguments :: Getopt::Std and Getopt::Long
* Perl provides getopt-like functionality using the packages Getopt::Std and Getopt::Long. For example:
use Getopt::Std;
getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts
getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
# Sets opt_* as a side effect.
getopts('oif:', \%opts); # options as above. Values in %opts
This example is taken from the man pages. getopt works much in the same way as getopt in C, but simpler. In the last example, getopts will take in -o, -i, -f with -f having arguments. The hash %opts contains the arguments, with the switches as the keys for the hash.
* Getopt::Long can take in switches longer than one character.
Tangent :: Using Packages/Modules
* You've probably noticed by now that we can call libraries, packages, or modules (whatever you want to call them), using the use keyword followed by the module.
* In the above example, we used the Getopt::Std package. There are tons of standard modules and even more non-standard modules. Think of these as ANSI C Standard libraries and non-standard libraries for Perl. Other packages include File::Copy, Text::ParseWords, Math::Complex, and CGI. There are many, many more. See Schwartz Pgs. 238-242, and Wall Ch. 30-32 (pgs. 831-915).
No comments:
Post a Comment