| 1 | # Build::Hopen::Scope::Hash - a hash-based nested key-value store based | ||||||||||||||||||||||||||||||||||||||||||
| 2 | package Build::Hopen::Scope::Hash; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use Build::Hopen::Base; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | our $VERSION = '0.000008'; # TRIAL | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use parent 'Build::Hopen::Scope'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Class::Tiny { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 21 | ▶ | _content => sub { +{} }, # Our storage | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | }; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | #use Build::Hopen::Util::Data qw(clone); | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use Build::Hopen::Arrrgs; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use Set::Scalar; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | #use Sub::ScopeFinalizer qw(scope_finalizer); | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | # Docs {{{1 | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | Build::Hopen::Scope::Hash - a hash-based nested key-value store | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | This class implements L<Build::Hopen::Scope> using a single hash table as the | ||||||||||||||||||||||||||||||||||||||||||
| 27 | storage. It only supports one set of data (L<Build::Hopen::Scope/$set>), | ||||||||||||||||||||||||||||||||||||||||||
| 28 | which is named C<0>. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | =head2 outer | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | The fallback C<Scope> for looking up names not found in this C<Scope>. | ||||||||||||||||||||||||||||||||||||||||||
| 35 | If non is provided, it is C<undef>, and no fallback will happen. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =head2 name | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | Not used, but provided so you can use L<Build::Hopen/hnew> to make Scopes. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | See also L</add>, below, which is part of the public API. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | Several of the functions receive a C<$levels> parameter. Its meaning is: | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | If C<$levels> is provided and nonzero, go up that many more levels | ||||||||||||||||||||||||||||||||||||||||||
| 52 | (i.e., C<$levels==0> means only return this scope's local names). | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 55 | If C<$levels> is not provided or not defined, go all the way to the | ||||||||||||||||||||||||||||||||||||||||||
| 56 | outermost Scope. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # }}}1 | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # Don't support -set, but permit `-set=>0` for the sake of code calling | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # through the Scope interface. Call as `_set0($set)`. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # Returns truthy of OK, falsy if not. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # Better a readily-obvious crash than a subtle bug! | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 127 | ▶ | sub _set0 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set0: called CC=5 SCAR=19.4
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 127 | ▶ | $_[0] //= 0; # Give the caller a default set | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_[0] //= 0
MC/DC0%0/1
$_[0] //= 0
$_[0]
Truth table50%1/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 127 | my $set = shift; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 127 | ▶ | return false if defined($set) && $set ne '0' && $set ne '*'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $set and $set ne 0 and $set ne "*"
defined $set and $set ne 0
MC/DC0%0/3
defined $set and $set ne 0 and $set ne "*"
defined $set
$set ne 0
$set ne "*"
Truth table25%1/4
A and B and C
A= defined $setB= $set ne 0C= $set ne "*"
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 127 | return true; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } #_set0() | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | =head1 FUNCTIONS TO BE OVERRIDDEN IN SUBCLASSES | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | To implement a Scope with a different data-storage model than the hash | ||||||||||||||||||||||||||||||||||||||||||
| 78 | this class uses, subclass Scope and override these functions. Only L</add> | ||||||||||||||||||||||||||||||||||||||||||
| 79 | is part of the public API. | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | =head2 add | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | Add key-value pairs to this scope. Returns the scope so you can | ||||||||||||||||||||||||||||||||||||||||||
| 84 | chain. Example usage: | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | my $scope = Build::Hopen::Scope::Hash->new()->add(foo => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | C<add> is responsible for handling any conflicts that may occur. In this | ||||||||||||||||||||||||||||||||||||||||||
| 89 | particular implementation, the last-added value for a particular key wins. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | TODO add $set option | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 13 | ▶ | sub add { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 13 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 13 | ▶ | croak "Got an odd number of parameters" if @_%2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 13 | my %new = @_; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 13 | @{$self->_content}{keys %new} = values %new; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 13 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } #add() | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =head2 adopt_hash | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | Takes over the given hash to be the new contents of the Scope::Hash. | ||||||||||||||||||||||||||||||||||||||||||
| 106 | Usage example: | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | $scope->adopt_hash({ foo => 42 }); | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | The scope uses exactly the hash passed, not a clone of it. If this is not | ||||||||||||||||||||||||||||||||||||||||||
| 111 | applicable to a subclass, that subclass should override it as C<...> or an | ||||||||||||||||||||||||||||||||||||||||||
| 112 | express C<die>. | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub adopt_hash { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
adopt_hash: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my $hrNew = shift or croak 'Need a hash to adopt'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | croak 'Cannot adopt a non-hash' unless ref $hrNew eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $self->_content($hrNew); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } #adopt_hash() | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | =head2 _names_here | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | Populates a L<Set::Scalar> with the names of the items stored in this Scope, | ||||||||||||||||||||||||||||||||||||||||||
| 127 | but B<not> any outer Scope. Called as: | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | $scope->_names_here($retval[, $set]); | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | No return value. | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 50 | ▶ | sub _names_here { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_names_here: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 50 | my ($self, %args) = parameters('self', [qw(retval ; set)], @_); | |||||||||||||||||||||||||||||||||||||||||
| 137 | 50 | ▶ | _set0 $args{set} or croak 'I only support set 0'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 50 | $args{retval}->insert(keys %{$self->_content}); | |||||||||||||||||||||||||||||||||||||||||
| 139 | } #_names_here() | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | =head2 _find_here | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | Looks for a given item in this scope, but B<not> any outer scope. Called as: | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | $scope->_find_here($name[, $set]) | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | Returns the value, or C<undef> if not found. | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 77 | ▶ | sub _find_here { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_here: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 77 | my ($self, %args) = parameters('self', [qw(name ; set)], @_); | |||||||||||||||||||||||||||||||||||||||||
| 153 | 77 | ▶ | _set0 $args{set} or croak 'I only support set 0'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 77 | my $val = $self->_content->{$args{name}}; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 77 | ▶ | return undef unless defined $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 33 | ▶ | return ($args{set} eq '*') ? { 0 => $val } : $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | } #_find_here() | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 161 | __END__ | ||||||||||||||||||||||||||||||||||||||||||