Hi namespace keepers,
I'm requesting the new module name: Getargs::Long (by analogy with
Getopt::Long).
It allows one to easily parse/extract information from routines called
with a set of -argument => value pairs.
Here's the interface I came up with:
use Getargs::Long; # case sensitive, exports [cx]+getargs
use Getargs::Long qw(ignorecase); # case insensitive
sub myroutine {
my (@x) = @_; # (-val => something, -other => xxx)
# Simple, args mandatory
my ($val, $other) = getargs(\@x, qw(val other));
# Simple, args optional (in [] means optional)
my ($val, $other) = getargs(\@x, [qw(val other)]);
# Simple with typechecking, args mandatory
my ($val, $other) = getargs(\@x, qw(val=Class::X other=ARRAY));
# Simple with typechecking, args optional
my ($val, $other) = getargs(\@x, [qw(val=Class::X other=ARRAY)]);
# Extract remainaing unparsed args in @extra
my ($val, $other, @extra) = getargs(\@x, qw(val other));
# Other cases, use full specs:
my ($x, $y, $z, $a, $b, $c) = xgetargs(\@x,
# Non-mandatory, defaults to undef unless specified otherwise
'x' => ['i'], # integer, no default
'y' => ['ARRAY', ['a', 'b']], # Has a default
'z' => [], # No typecheck, can be anything
# Mandatory arguments
'a' => 'i', # integer (scalar)
'b' => 'TYPE', # TYPE or any ancestor of TYPE
'c' => undef, # unspecified type but mandatory
);
# Cache version -- parsing anonymous routine created and saved
my ($val, $other) = cgetargs(\@x, qw(val other));
}
Raphael