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

In reply to Re: How to pass and retrieve command-line arguments specifying search conditions? by benrwebb
in thread How to pass and retrieve command-line arguments specifying search conditions? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.