Pida
Pida

Reputation: 15

Getopt::Long passing several arguments to a subroutine

How to pass several arguments from command line to a function in Getopt::Long? My problem is as follows. I define options in the following way:

...

my $result = GetOptions('ham=s{2}' => \&hamming_distance); 

...

sub hamming_distance {
my @values = @_;
...
}

If I call the program with option

--ham good wood

I got two calling of the subroutine hamming distance, once with "good" and once with "wood", i.e, the value of @_ is ham,good and then ham,wood. How can I get only one call with both parameters - ham,good,wood?

Upvotes: 0

Views: 140

Answers (2)

ikegami
ikegami

Reputation: 385657

my @ham;
my $result = GetOptions('ham=s{2}' => \@ham)
   or usage();

hamming_distance(@ham) if @ham;

Upvotes: 2

toolic
toolic

Reputation: 62037

If you don't need a handler subroutine, you could simple create an array:

use warnings;
use strict;
use Getopt::Long;
use Data::Dumper;

my @values;
my $result = GetOptions('ham=s{2}' => \@values); 
print Dumper(\@values);

__END__

$VAR1 = [
          'good',
          'wood'
        ];

Note that this array method is experimental, according to the documentation.

Upvotes: 1

Related Questions