$0 is the name of your script when it is running (just like shell).
@ARGV is an array that holds the arguments the user gave your script when it was ran (remember that $1, $2.. etc in Perl have to do with regexs, not arguments).
Use the standard Getopt::Std module to Perl to process commandline switches from the user. Getopt::Long (not discussed) lets you use extended options rather than single letter ones.
If you "use Getopt::Std;" in your program you can use either "getopt" or "getopts". The first lets the user use any letters in addition to the ones you spell out, the second only lets them use the ones you spell out, and is the version that lets you use colons to specify additional args.
#!/usr/bin/perl -w
use Getopt::Std;
getopts('abc:', \%opts);
if (defined $opts{'a'}) {
print "option a selected\n";
}
if (defined $opts{'b'}) {
print "option b selected\n";
}
if (defined $opts{'c'}) {
print "option c selected\n";
print "it had $opts{'c'} attached\n";
}
Run with something like:
$ checkopts.pl -a
$ checkopts.pl -a -b
$ checkopts.pl -ba
$ checkopts.pl -a -c foo -b
Use the value of the hash entry to get the OPTARG attached.
getopt/getopts take an optional second arg, a reference to a hash, which will define key/value pairs to represent arguments found. If you don't supply that, it will define $opt_x for each one found, where x is the argument.
Automatically consumes args, no need to shift.
The Perl interpreter, perl, can take quite a few commmandline arguments/switches to alter it's behavior.
See "man perlrun" for a complete listing, but here are a few of the more interesting ones...
Some Perl arguments can be grouped into a class that modifies the behavior of the interpreter or provides alternate commands to run. These are used to build "one-liners", scripts written on the commandline rather than in a file.
LINE: while (<>) {
# your code here
}
LINE: while (<>) {
# your code here
print;
}
% perl -e 'print "hi\n"' ; hi % more data greg long greg % perl -pe 's/greg/long/g' data long long long % perl -i.bak -pe 's/greg/x/g' data % more data x long x % more data.bak greg long greg %
These get considerably uglier the better you get.