# Takes a hashref, a key name, and an optional default.
# Removes that key from the hash, and returns the value or
# the default. Blows up if there is no value or default.
sub excise_arg {
my $args = shift;
my $arg_name = shift;
if (exists $args->{$arg_name}) {
return delete $args->{$arg_name};
}
elsif (@_) {
return shift;
}
else {
confess("Missing required argument '$arg_name'");
}
}
####
# Takes a hashref. Verifies that it is empty
sub assert_args_done {
my @left = keys %{ $_[0] };
if (@left) {
confess("Unexpected arguments '@left' left over");
}
}
##
##
sub some_func {
my $args = { @_ };
my $name = excise_arg($args, 'name'); # required
my $age = excise_arg($args, 'age', undef); # optional
assert_args_done($args);
# Rest of the code here.
}