Named parameters with default values for subroutines
sub foo {
my %args = (
# set your defaults/doc your accepted args here
arg1 => 'default'
arg2 => undef,
# passed-in args replace your defaults
@_
);
# also allows for quick checking for missing required args
for (keys %args) {
unless ( $args{$_} ) {
carp("Required argument '$_' was missing from arguments:" . Dumper(\%args));
return;
}
}
}
Written by petec
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Perl
Authors
janosgyerik
25.11K
Jean-Remy Duboc
12.22K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#