use strict; use warnings; use Data::Dumper; my $i=0; my %hARGV = map { if (/^--(\w+)=(\w+)$/) { # option => value $1 => $2; } elsif (/^--(\w+)$/) { # flag => 1 $1 => 1 } elsif (m{^[\w/]}) { # filename => fileorder $_ => $i++; } else { # bad argument - add nothing to the result hash warn "Invalid option: <$_> - options must begin with " ."-- or be a legal file name"; (); } } @ARGV; print Dumper(\%hARGV); #### use strict; use warnings; use Data::Dumper; my @aFiles; # <== array instead of my $i my %hARGV = map { if (/^--(\w+)=(\w+)$/) { # option => value $1 => $2; } elsif (/^--(\w+)/) { # flag => 1 $1 => 1 } elsif (m{^[\w/]}) { push @aFiles, $_; # <== put filenames into an array (); # <== don't put anything into hash (yet) } else { # bad argument - add nothing to the result hash warn "Invalid option: <$_> - options must begin with " ."-- or be a legal file name"; (); } } @ARGV; $hARGV{files}=\@aFiles if @aFiles; # <== _now_ put the files in the hash print Dumper(\%hARGV); #### use strict; use warnings; use Data::Dumper; sub processArgs { my ($sArg, $aFiles) = @_; if ($sArg =~ /^--(\w+)=(\w+)$/) { # option => value $1 => $2; } elsif ($sArg =~ /^--(\w+)/) { # flag => 1 $1 => 1 } elsif ($sArg =~ m{^[\w/]}) { push @$aFiles, $_; (); } else { # bad argument - add nothing to the result hash warn "Invalid option: <$_> - options must begin with " ."-- or be a legal file name"; (); } } my @aFiles; my %hARGV = map { processArgs($_, \@aFiles) } @ARGV; $hARGV{files}=\@aFiles if @aFiles; print Dumper(\%hARGV);