use strict; use Getopt::Std; use Data::Dumper; sub parse_cmd{ my $cmdline = shift; local @ARGV; @ARGV = split('\s+',$cmdline); shift @ARGV; my %options; getopts("x:y:z:abc", \%options); return %options; } my %opts = parse_cmd("someprg -x foo -y bar -z baz -ab"); print Dumper(\%opts); #### $VAR1 = { 'x' => 'foo', 'y' => 'bar', 'a' => 1, 'z' => 'baz', 'b' => 1 };