#!/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) {
...
}
####
# the short form
script.pl -a -r -d
script.pl -a -d
...
# the long form
script.pl --add --remove --delete
script.pl --delete
...
####
#!/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 <
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";
}
}
####
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