tags:

views:

39

answers:

3

For Example :

use Getopt::Long;
%file ;
GetOptions('file=s%' =>
sub { push(@{$file{$_[1]}}, $_[2]) });

use Data::Dumper ;
print Dumper %file ;


print  @{$file{filename}} ;

my @file_array = @{$file_ref};

print "==\n @file_array == ";

I can execute and it working :

 perl multipls.pl  --file filename=a.txt  --file  filename=b.txt filename=c.txt

I am looking for

 perl multipls.pl  --file filename=a.txt  filename=b.txt filename=c.txt

How to acheive this ?

+1  A: 
use strict;
use warnings;
use Getopt::Long;

my @files;

GetOptions ( "file=s{,}" => \@files );

Side Note:

The example in the problem doesn't really make sense. If all the filenames are going to be pushed to the 'filename' key, does it make sense to have a hash?

Zaid
When I run your code with the OP's command line, the array only contains the 1st file, not all 3 files.
toolic
@toolic : Can you try it with the `s{,}` option?
Zaid
Yes, that works.
toolic
A: 

You don't have use warnings; turned on. If you did, you'd see that you are not actually declaring anything with this line:

%file;

You'd want that to be

my %file;

But as another poster said, you don't want it in a hash anyway. I'm just pointing out the value of use warnings;.

Andy Lester
+2  A: 

I don't know how to convince Getopt::Long to do exactly what you are asking, but I often use shell quoting to group several items into one string, then split the string into an array:

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

my @files;
my $filelist;
GetOptions('file=s' => \$filelist);

if ($filelist) {
    $filelist =~ s/^\s+//; # Remove any leading whitespace
    @files = split /\s+/, $filelist;
}
print Dumper(\@files);

__END__


perl multipls.pl --file "filename=a.txt filename=b.txt"

$VAR1 = [
          'filename=a.txt',
          'filename=b.txt'
        ];
toolic