I am trying to setup Getopt::Long
to handle the arguments from a configuration script.
Here is my starter:
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my $config_file = '';
GetOptions (
'config|c=s' => \$config_file,
'add|a' => \&add_server,
'del|d' => \&del_server,
);
sub add_server {
print "$config_file\n";
}
sub del_server {
# Left blank for now.
}
The odd thing is I am running into a problem when I run my script with something like this:
./config.pl -a -c config.xml
It does NOT print the -c
option, but if I run it like this,
./config.pl -c config.xml -a
it works like it should.
I think I understand the reason why; it has to do with the order execution right?
How can I fix it? Should I use Getopt::Long
in conjunction with @ARGV
?
Ultimately, I am trying to make the command line args pass into the subroutine that I am calling. So if -a
or --add
, I want the options of -c
or --config
to pass into the subroutine when it is called.
Any ideas?