use warnings; use strict; use Data::Dump; use Getopt::Std; use Getopt::Long; # fake command line local @ARGV = qw/ one -abc -- two -d -e -- three -abcdef -- four --foo --bar --bar --bar -- five --foo --quz baz /; while (@ARGV) { my $cmd = shift @ARGV; getopts('abcdef', \my %o); dd $cmd, \%o; last if $o{f}; } while (@ARGV) { my $cmd = shift @ARGV; GetOptions(\my %o, 'foo!', 'bar+', 'quz!'); dd $cmd, \%o; last if $o{quz}; } dd @ARGV; __END__ ("one", { a => 1, b => 1, c => 1 }) ("two", { d => 1, e => 1 }) ("three", { a => 1, b => 1, c => 1, d => 1, e => 1, f => 1 }) ("four", { bar => 3, foo => 1 }) ("five", { foo => 1, quz => 1 }) "baz"