[PATCH 3/5] Added config_default($key, $default) to Git.pm
- From
- Dan Zwell <dzwell@zwell.net>
- Date
- Nov 22, 2007, 10:55 UTC
- Message-ID
- <20071122045552.30ca55c2@paradox.zwell.net>
- In-Reply-To
- <20071110180109.34febc3f@paradox.zwell.net>
Method returns a configuration value if defined, or the default value that was passed in, otherwise.
The main purpose of this method is to allow the empty string to be a valid configuration option, and to replace the following construct:
$val = $repo->config('my.key') || $default_valSigned-off-by: Dan Zwell <dzwell@zwell.net> --- perl/Git.pm | 28 ++++++++++++++++++++++++++++ 1 files changed, 28 insertions(+), 0 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm index 6603762..7327300 100644 --- a/perl/Git.pm +++ b/perl/Git.pm @@ -549,6 +549,34 @@ sub config_bool { }; } +=item config_default ( VARIABLE, DEFAULT ) + +Fetches a configuration option C<VARIABLE>, returning its +value if it is defined (it is valid to return the empty string, +if that is its value). Otherwise, C<DEFAULT>, a default +value, is returned. This method may be a replacement for + + my $value = $repo->config('my.key') || 'default val'; + +in situations where the empty string is an acceptable return value. +This method may also be called in a vector context, when expecting +multivars. + + my @value = $repo->config_default('my.multivar', \@default_vals); + +=cut + +sub config_default { + my ($self, $var, $default) = @_; + if (wantarray) { + my @value = $self->config($var); + return @value ? @value : @$default; + } + else { + my $value = $self->config($var); + return (defined $value) ? $value : $default; + } +} =item ident ( TYPE | IDENTSTR )
-- 1.5.3.5.565.gf0b83-dirty