From 728992688946fe88173e7cd561456a10c9d23fd8 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Tue, 18 Dec 2018 12:24:44 +0200 Subject: Fix config->as_hash * lib/App/Glacier/Config.pm (as_hashref): New method. (as_hash): Return a hash. --- lib/App/Glacier/Command.pm | 7 ++++--- lib/App/Glacier/Config.pm | 28 ++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/App/Glacier/Command.pm b/lib/App/Glacier/Command.pm index 62a236b..7178a75 100644 --- a/lib/App/Glacier/Command.pm +++ b/lib/App/Glacier/Command.pm @@ -143,7 +143,7 @@ sub new { } } - $self->{_glacier} = new App::Glacier::Bre(%{$self->config->as_hash('glacier')//{}}); + $self->{_glacier} = new App::Glacier::Bre($self->config->as_hash('glacier')); if ($self->{_glacier}->lasterr) { $self->abend(EX_CONFIG, $self->{_glacier}->last_error_message); } @@ -183,7 +183,7 @@ sub jobdb { my $be = $self->cfget(qw(database job backend)); $self->{_jobdb} = new App::Glacier::Roster( $be, - %{$self->config->as_hash(qw(database job)) // {}} + $self->config->as_hash(qw(database job)) ); } return $self->{_jobdb}; @@ -212,7 +212,8 @@ sub directory { $be, $vault_name, $self->glacier, - %{$self->config->as_hash(qw(database inv)) // {}}); + $self->config->as_hash(qw(database inv)) + ); } return $self->{_dir}{$vault_name}; } diff --git a/lib/App/Glacier/Config.pm b/lib/App/Glacier/Config.pm index 33d54d2..63dd61c 100644 --- a/lib/App/Glacier/Config.pm +++ b/lib/App/Glacier/Config.pm @@ -737,7 +737,16 @@ sub get { return $ref; } -sub as_hash { +=head2 $cfg->as_hashref(@path) + +If I<@path> represents a section, convert that section to a perl hash, +and return a reference to that hash. + +If I<@path> does not exist or refers to a value, return C. + +=cut + +sub as_hashref { my $self = shift; my $ref = $self->getref(@_); my $hroot = {}; @@ -755,7 +764,22 @@ sub as_hash { $elt->[2]{$elt->[0]} = $elt->[1]->{-value}; } } - return $hroot->{''}; + my $r = $hroot->{''}; + return ref($r) eq 'HASH' ? $r : undef; +} + +=head2 $cfg->as_hashref(@path) + +If I<@path> represents a section, return that section converted to a +perl hash. + +If I<@path> does not exist or refers to a value, the returned hash is +empty. + +=cut + +sub as_hash { + return %{shift->as_hashref(@_) // {}} } =head2 $cfg->isset(@path) -- cgit v1.2.1