With that syntax, your shell is likely to eat the && and ||. You should put quotes around that part and parse @ARGV yourself.
perl search.pl 'arg1 && arg2 || arg3'
(The quotes used may vary with OS)Or you could use a different syntax like this:
perl search.pl arg1 AND arg2 OR arg3
YMMV
| [reply] [d/l] [select] |
Arguments will be in the array @ARGV. For example:
our($arg1,$op,$arg2) = @ARGV;
would read the first three arguments from the command-line into $arg1, $op, and $arg2. For more advanced command-line processing, you can use Getopt::Std or Getopt::Long.
As duff said, certain characters will be interpreted by the shell and need to be escaped or avoided.
| [reply] [d/l] [select] |
Ok, for command line args, by far the best way to do it (IMHO) is to use Getopt, but it will require you to change your syntax a little. Here is some example (sortof tested) code:
use Getopt::Std;
# set up informative strings
my $usage = "$0 version $VERSION by -= $AUTHOR =-
use -h for help";
my $help = "use $0 {-a | -o} <search1> <search2>
use $0 -a to "AND" your criteria
use $0 -o to "OR" your criteria
";
#when you call getopts, you pass a string and a hash ref.
# the string defines what switches you accept, and the hash
# gets filled in accordingly. For instance, if someone put
# in a -h here, $options{h} would be set to 1 (true).
my %options;
getopts('huao', \%options);
# in this case, I'm assuming either and "and" or "or" is
# required.
$options{u}=1 unless %options && ($options{a} || $options{o});
#it's more readable to break this into two tests
#we need to make sure someone didn't put in -a and -o
$options{u}=1 if ($options{a} && $options{o});
# I haven't done it this way in a while, but IIRC
# anything not handled by getopts gets left in ARGV
# Therefore, we need to make sure that there are two
# options left (two criteria)
$options{u}=1 unless $#ARGV==2;
# now here we go. for any of our test cases we would
# have set $options{u}, so if the command line was bad
# it will get caught and exited here.
if ($options{h} || $options{u}){
print "$usage\n";
print "$help\n" if ($options{h});
exit 0;
}
From here I hope it's pretty obvious. Either $options{o} or $options{a} is set, and the criteria just need to be assigned out of ARGV.
Like I said, the code is untested. It comes from my boilerplate new script template, but I've never tested this particular writeup.
Take some time to read the documentation for getopt::std and getopt::long, they are worth keeping in your perl arsenal | [reply] [d/l] |
Getopt::Mixed is my favorite module in the Getopt namespace. It's easy and has many features.
| [reply] |