Making copies of @ARGV will keep you busy parsing again and again the same set of options. If this is what you want, fine. But.
Suppose you have a set of modules you are using in your program which can recognize only a subset of your @ARGV. What you want to do is to call GetOptions from each of these module, consume each of the options the module is able to recognize and leave the rest of the options in @ARGV to be processed by the other modules.
You can configure Getopt::Long to do this by calling
Getopt::Long::Configure qw/pass_through/;
But see perldoc Getopt::Long for various configurations side effects!
Example: a script (o1.pl) able to recognize few options and two modules (o1::p1 and o1::p2) which must get to read their own options
o1.pl:
!/usr/bin/perl
use Getopt::Long;
use o1::p1;
use o1::p2;
# now o1::p1 and o1::p2 already consumed recognizable options
#no need to configure pass_through since main:: will get to see only its options
#Getopt::Long::Configure(qw/pass_through/);
my %main_options = ( 'define' => {}, );
print "main: \@ARGV = " . join (', ', @ARGV) . "\n";
GetOptions(\%main_options, "main-vi=i","verbose",'define=s');
use Data::Dumper;
print "main_options: ", Dumper(\%main_options);
print "p1 options: ", Dumper(\%o1::p1::options);
print "p2 options: ", Dumper(\%o1::p2::options);
exit 0;
o1::p1 source (in o1/p1.pm):
package o1::p1;
use Getopt::Long;
Getopt::Long::Configure qw/pass_through/;
%options = ();
print "package p1: \@ARGV = " . join (', ', @ARGV) . "\n";;
GetOptions(\%options, "p1-v1=s", "p1-v2=i");
1;
o1::p2 source (in o1/p2.pm):
package o1::p2;
use Getopt::Long;
Getopt::Long::Configure 'pass_through';
%options = ();
print "package p2: \@ARGV=". join (', ', @ARGV). "\n";
GetOptions(\%options, "p2-v1=s", "p2-v2=i");
1;
running o1.pl with:
perl o1.pl --main-vi=1 --verbose --define a=ss --p1-v1=k1 --p1-v2=42 --define b=yy --p2-v1=k2 --p2-v2=66
will give you the following (expected) output (p1 consumed its options, then p2 did it, then main was left with what it knows about):
package p1: @ARGV = --main-vi=1, --verbose, --define, a=ss, --p1-v1=k1, --p1-v2=42, --define, b=yy, --p2-v1=k2, --p2-v2=66
package p2: @ARGV=--main-vi=1, --verbose, --define, a=ss, --define, b=yy, --p2-v1=k2, --p2-v2=66
main: @ARGV = --main-vi=1, --verbose, --define, a=ss, --define, b=yy
main_options: $VAR1 = {
'verbose' => 1,
'define' => {
'a' => 'ss',
'b' => 'yy'
},
'main-vi' => 1
};
p1 options: $VAR1 = {
'p1-v1' => 'k1',
'p1-v2' => 42
};
p2 options: $VAR1 = {
'p2-v1' => 'k2',
'p2-v2' => 66
};