Lets take a simple example: I wanto to have the following input in my script:
[
{foo => date_1},
{foo2=> date_2},
{foo3=> undef}, # there was no input
... # probably more in the future
]
So using something similar to this:
use strict;
use warnings;
use utf8;
use Getopt::Long qw(GetOptions);
my @foos;
my @dates;
# INPUT
GetOptions(
'foo|f:s' => \@foos,
'date|d:s' => \@dates,
# 'help|h' => \&help,
) or die "Invalid options passed to $0\n";
I want to be able to call the script with somehing similar to this:
perl script.pl --foo "Go with 1" --foo "Go with 2" --date "date1" --date "date2"
And then be able to do somethig like:
foreach my $i (0..scalar(@foos)){
print $foos[$i] . " " . $dates[$i] . "\n";
}
And obtain:
Go with 1 date1
Go with 2 date2
Why I want to do this?: I loop over an uncertain amount of foos
for one function. In another function I want to loop again through the foos and print a date associated to it if present.
What I would like to avoid: Having to create a flag for every element of foos, like the following:
GetOptions(
'foo|f:s' => \@foos,
'date-foo-1|df1:s' => \$dates_foo_1,
'date-foo-2|df2:s' => \$dates_foo_2,
# 'help|h' => \&help,
) or die "Invalid options passed to $0\n";
I think what I would to be able to do is having an optional flag associated to another flag but I have not been able to find anything related.