Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can a wise monk show an example of how to set arguements for a command line script? I'm trying to setup a search script where it will open a text file and given one or more arguements, search the file.

Something where I can go  perl search.pl search_arguement1 && search_arguement2 or switch && for ||. Asking for one at a time from STDIN is possible and a lot easier, but everyone else does it on one line. How do you do this?

20040519 Edit by castaway: Changed title from 'arguements'

  • Comment on How to pass and retrieve command-line arguments specifying search conditions?
  • Download Code

Replies are listed 'Best First'.
Re: How to pass and retrieve command-line arguments specifying search conditions?
by duff (Parson) on May 18, 2004 at 18:24 UTC
    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
Re: How to pass and retrieve command-line arguments specifying search conditions?
by sgifford (Prior) on May 18, 2004 at 18:38 UTC
    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.

Re: How to pass and retrieve command-line arguments specifying search conditions?
by benrwebb (Scribe) on May 18, 2004 at 21:18 UTC
    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
Re: How to pass and retrieve command-line arguments specifying search conditions?
by danb (Friar) on May 19, 2004 at 05:33 UTC
    Getopt::Mixed is my favorite module in the Getopt namespace. It's easy and has many features.

    -Dan