Getopt::Std (which you use but never use) gets you most of the way there.
#!/usr/bin/perl -w use strict; use Getopt::Std; use Data::Dumper; my %switches; getopts('x:o:d:m:', \%switches); print Dumper \%switches;
Running it gets you:
$ ./test.pl -o outputfile -x xmlfiles -d disk1 -m memory1 $VAR1 = { 'm' => 'memory1', 'd' => 'disk1', 'x' => 'xmlfiles', 'o' => 'outputfile' };
The problem is, of course, with your multi-valued options. If you could make one small change to the way you pass them into the program then this would be very simple.
#!/usr/bin/perl -w use strict; use Getopt::Std; use Data::Dumper; my %switches; getopts('x:o:d:m:', \%switches); foreach (keys %switches) { $switches{$_} = [ split /\s+/, $switches{$_} ]; } print Dumper \%switches;
The one problem is that you have to quote the multiple keys like this:
--$ ./test.pl -o outputfile -x xmlfiles -d 'disk1 disk2' -m 'memory1 mem +ory2' $VAR1 = { 'm' => [ 'memory1', 'memory2' ], 'd' => [ 'disk1', 'disk2' ], 'x' => [ 'xmlfiles' ], 'o' => [ 'outputfile' ] };
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
In reply to Re: Creating Hashes from Switches
by davorg
in thread Creating Hashes from Switches
by johnirl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |