: I tried to split the arguments manipulation to two parts.
:
: [snip code]
:
: It looks like only the first "GetOptions" command processed (getA)
: since I get "Unknown option" message for all arguments in second
: subroutine (getB).
:
: Is there any way to make both subroutines get the options ?
: Do I have to use the "GetOptions" command only once?
Making the code a little more chatty shows what's going on:
#! /usr/local/bin/perl
use warnings;
use strict;
use Getopt::Long;
$Getopt::Long::debug = 1;
&getA;
&getB;
sub getA {
my($a1,$a2,$a3);
my $result = GetOptions (
"a1" => \$a1,
"a2" => \$a2,
"a3" => \$a3,
);
if ($result) {
$_ = defined $_ ? $_ : "<undef>" for ($a1, $a2, $a3);
print "\$a1 = $a1\n",
"\$a2 = $a2\n",
"\$a3 = $a3\n";
}
else {
print "getA: no dice\n";
}
}
sub getB {
my($b1,$b2,$b3);
my $result = GetOptions (
"b1" => \$b1,
"b2" => \$b2,
"b3" => \$b3,
);
if ($result) {
$_ = defined $_ ? $_ : "<undef>" for ($b1, $b2, $b3);
print "\$b1 = $b1\n",
"\$b2 = $b2\n",
"\$b3 = $b3\n";
}
else {
print "getB: no dice\n";
}
}
Watch what happens:
$ ./try --b3
Getopt::Long 2.34 ($Revision: 2.68 $) called from package "main".
ARGV: (--b3)
autoabbrev=1,bundling=0,getopt_compat=1,gnu_compat=0,order=1,
ignorecase=1,requested_version=0,passthrough=0,genprefix="(--|-|\+)".
=> link "a1" to SCALAR(0x101d728c)
=> link "a2" to SCALAR(0x101d7268)
=> link "a3" to SCALAR(0x101d72a4)
=> $opctl{a1} = ARRAY(0x101ebb88) ["","a1",M,$,"<undef>"]
$opctl{a2} = ARRAY(0x1014c900) ["","a2",M,$,"<undef>"]
$opctl{a3} = ARRAY(0x1014c924) ["","a3",M,$,"<undef>"]
=> arg "--b3"
=> find "--b3"
=> split "--"+"b3"
=> 0 hits () with "b3" out of 3
Unknown option: b3
getA: no dice
Getopt::Long 2.34 ($Revision: 2.68 $) called from package "main".
ARGV: ()
autoabbrev=1,bundling=0,getopt_compat=1,gnu_compat=0,order=1,
ignorecase=1,requested_version=0,passthrough=0,genprefix="(--|-|\+)".
=> link "b1" to SCALAR(0x101eba2c)
=> link "b2" to SCALAR(0x101eba44)
=> link "b3" to SCALAR(0x101eba5c)
=> $opctl{b1} = ARRAY(0x1014d474) ["","b1",M,$,"<undef>"]
$opctl{b3} = ARRAY(0x1014c924) ["","b3",M,$,"<undef>"]
$opctl{b2} = ARRAY(0x10149948) ["","b2",M,$,"<undef>"]
$b1 = <undef>
$b2 = <undef>
$b3 = <undef>
As you can see, the first call to Getoptions empties @ARGV. What
benefit would you see in processing options in separate places?
Hope this helps,
Greg