My goal is to have a parameter --override=fthat controls the values ββof two other parameters. The trick is how to refer to the parameter value (the part corresponding fin the pointer =f) in subwhich is executed when GetOptions detects the presence of an option on the command line.
Here is how I do it:
$ cat t.pl
use strict;
use warnings;
use Getopt::Long;
our %Opt = (
a => 0,
b => 0,
);
our %Options = (
"a=f" => \$Opt{a},
"b=f" => \$Opt{b},
"override=f" => sub { $Opt{$_} = $_[1] for qw(a b); },
);
GetOptions(%Options) or die "whatever";
print "\$Opt{$_}='$Opt{$_}'\n" for keys %Opt;
$ t.pl --override=5
$Opt{a}='5'
$Opt{b}='5'
$ t.pl --a=1 --b=2 --override=5 --a=3
$Opt{a}='3'
$Opt{b}='5'
The code seems to process the parameters and redefine as I want. I found that sub, $_[0]with options name (full name, even if it is reduced in the command line), and $_[1]contains the value. Magic.
I have not seen this documented, so I am worried if I am not mistaken when using this technique.