Skip to content

Instantly share code, notes, and snippets.

@lepht lepht/args.pl
Last active Dec 11, 2015

Embed
What would you like to do?
Perl named, self-documenting subroutine arguments with default values
# Perl named, self-documenting subroutine arguments with default values
sub foo {
my %args = (
# Set your defaults/doc your accepted args here
arg1 => 'default'
arg2 => undef,
somelist => [qw( some stuff )],
@_ # Passed-in args replace your defaults
);
# Do stuff...
}
# When calling your subroutine, just pass in the arguments as documented in the defaults.
# Any keys not specified will retain their default value:
foo( arg2 => 'bar', somelist => ['baz'] );
# Results in:
%args = (
'arg2' => 'bar',
'arg1' => 'default',
'somelist' => ['baz']
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.