What's wrong with if's? Personally I think they make your program much easier to read and understand...
#!/usr/local/bin/perl -w use Getopt::Long; use Pod::Usage; use strict; # Parse command line arguments and assign corresponding variables GetOptions ( 'a|add' => \( my $ADD = undef ), 'r|remove' => \( my $REMOVE = undef ), 'd|delete' => \( my $DELETE = undef ), ); unless ( defined $ADD or defined $REMOVE or defined $DELETE ) { pod2usage( -exitval => 1, -output => \*STDERR ); } if ($ADD) { ... } if ($REMOVE) { ... } if ($DELETE) { ... }
And you run the script with -
# the short form script.pl -a -r -d script.pl -a -d ... # the long form script.pl --add --remove --delete script.pl --delete ...
Update: Here's a different approach - provide a list of commands to monitor on the commandline, separated by commas.

#!/usr/local/bin/perl -w use Getopt::Long; use Data::Dumper; use strict; # Parse command line arguments and assign corresponding variables GetOptions ( 'm|monitor=s' => \( my $MON = undef ), ); unless ( defined $MON ) { print <<EOF Usage: $0 <-m|--monitor [ADD][,REMOVE][,DELETE]> Example: $0 -m ADD,DELETE $0 -m remove,delete Note: No space between comma and commands EOF ; exit(1); } my @options = map { uc } (split /,/, $MON); # verify input commands my @allowed_commands = qw/ ADD REMOVE DELETE /; foreach my $opt (@options) { die "Unknown command $opt" if ! scalar grep $opt eq $_, @allowed_commands; } my $monitor_commands = join '|', @options; # DEMO - give it a little test in action monitor... # @actions simulates a sequence of commands my @actions = qw/ ADD ADD REMOVE ADD DELETE FREE /; foreach my $action (@actions) { if ($action =~ /($monitor_commands)/) { print "Command [$action] is monitored\n"; } else { print "Command [$action] is NOT monitored\n"; } }
And the output for script.pl -m add,delete is -
Command [ADD] is monitored Command [ADD] is monitored Command [REMOVE] is NOT monitored Command [ADD] is monitored Command [DELETE] is monitored Command [FREE] is NOT monitored

In reply to Re: build regular expression from scalar? by Roger
in thread build regular expression from scalar? by shapi003

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.