in reply to Your named arguments
I don't enforce policies of "even if you want to pass in an argument with an undefined value, you must specify it" on my callers. I'm a Perl programmer, so my programs are programmer friendly. Absence of named parameters are fine. I don't balk at extra arguments either - this can be very useful, as wrappers can just call the function with whatever @_ they were called with.sub convert { my %arg = @_; my ($from, $to, $thing) = @arg{qw /from to thing/}; ... }
On the other hand, I might work with defaults. Then I'd write it as:
Or:sub convert { my %arg = @_; my $from = exist $arg{from} ? $arg{from} : 'default'; my $to = exist $arg{to} ? $arg{to} : 'default'; my $thing = exist $arg{thing} ? $arg{thing} : 'default'; ... }
Or when passing in a false value doesn't make sense, I'd make it that any false value means 'default':sub convert { my %arg = (from => 'default', to => 'default', thing => 'default', @_); my ($from, $to, $thing) = @arg{qw /from to thing/}; ... }
And when I want to do something fancy, I use Getopt::Long:sub convert { my %arg = @_; my $from = $arg{from} || 'default'; my $to = $arg{to} || 'default'; my $thing = $arg{thing} || 'default'; ... }
sub convert { local @ARGV = @_; GetOptions ('from:s' => \my $from, 'to:s' => \my $to, 'thing:s' => \my $thing); ... }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Your named arguments
by creamygoodness (Curate) on Nov 08, 2005 at 19:06 UTC | |
by adrianh (Chancellor) on Nov 09, 2005 at 12:08 UTC | |
by creamygoodness (Curate) on Nov 09, 2005 at 18:06 UTC |