| 1 | # Data::Hopen::Scope - a nested key-value store | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | package Data::Hopen::Scope; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use Data::Hopen::Base; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 18 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use Scalar::Util qw(refaddr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 18 | our $VERSION = '0.000018'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # Class definition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | use Class::Tiny { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 18 | outer => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | local => false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | name => 'anonymous scope', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | merge_strategy => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # Internal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | _first_set => undef, # name of the first set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | _merger_instance => undef, # A Hash::Merge instance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 18 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # Static exports | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 18 | ▶ | use vars::i '@EXPORT_OK_PUBLIC' => [qw(is_first_only)]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | use vars::i { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 18 | '@EXPORT' => [qw(FIRST_ONLY)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | '@EXPORT_OK' => [@EXPORT_OK_PUBLIC, qw(_set0)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 18 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 18 | use vars::i '%EXPORT_TAGS' => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 'default' => [@EXPORT], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 'all' => [@EXPORT, @EXPORT_OK_PUBLIC], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 'internal' => [qw(_set0)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 18 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 18 | my $_first_only = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | ▶ | sub FIRST_ONLY { $_first_only } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FIRST_ONLY: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 18 | ▶ | use constant _LOCAL => 'local'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # What we use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 18 | ▶ | use Config; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 18 | ▶ | use Data::Hopen qw(getparameters); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 18 | ▶ | use Data::Hopen::Util::Data qw(clone forward_opts); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 18 | ▶ | use Hash::Merge; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 18 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 18 | ▶ | use Set::Scalar; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 18 | ▶ | use Sub::ScopeFinalizer qw(scope_finalizer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # Docs {{{1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | Data::Hopen::Scope - a nested key-value store. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | A Scope represents a set of data available to operations. It is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | key-value store that falls back to an outer C<Scope> if a requested key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | isn't found. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | This class is the abstract base of Scopes. See L<Data::Hopen::Scope::Hash> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | for an example of a concrete implementation using a hash under the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | hood. Different subclasses use different representations. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | See L</"FUNCTIONS TO BE OVERRIDDEN IN SUBCLASSES"> for more on that topic. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | =head1 STATIC EXPORTS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | =head2 FIRST_ONLY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | A flag used as a L</$set> (q.v.). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | =head2 outer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | The fallback C<Scope> for looking up names not found in this C<Scope>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | If non is provided, it is C<undef>, and no fallback will happen. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | =head2 local | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | (Default falsy.) If truthy, do not go past this scope when doing local | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | lookups (see L</$levels> below). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | =head2 name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | Not used, but provided so you can use L<Data::Hopen/hnew> to make Scopes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | =head2 merge_strategy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | How the inputs of L</merge> will be treated. Case-insensitive. Note that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | changes after the first time you call L</merge> will be ignored! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | (TODO change this - just need a custom setter) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | Values are: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | =item C<undef> or C<'combine'> (default) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | L<Hash::Merge/Retainment Precedence>. Same-name keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | are merged, so no data is lost. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | =item C<'keep'> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | L<Hash::Merge/Left Precedence>. Existing data will not be replaced by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | new data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | =item C<'replace'> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | L<Hash::Merge/Right Precedence>. New data will replace existing data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | under a particular key will win. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | =head1 PARAMETERS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | The methods generally receive the same parameters. They are as follows. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | =head2 $name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | The name of an item to be looked up. Names must be truthy. That means, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | among other things, that C<'0'> is not a valid name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | =head2 $set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | A Scope can have multiple sets of data. C<$set> specifies which one to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | look in. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | If specified as a number or a name, look only in that set. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | If C<'*'>, look in every available set at this level, and return a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | hashref of C<< { set_name => value } >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | Note that this is not recursive --- it won't collect all instances | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | of the given name from all sets in all the levels. (TODO? change this?) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | If L</FIRST_ONLY>, look in only the first set (usually named C<0>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | If unspecified or undefined, look in every available set at this level, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | return the first one found, regardless of which set it comes from. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | =head2 $levels | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | How many levels up (L</outer>) to go when performing an operation. Note: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | chains more than C<POSIX::INT_MAX> (L<POSIX/LIMITS>) Scopes long may fail in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | unexpected ways, depending on your platform! For 32- or 64-bit platforms, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | that number is at least 2,000,000,000, so you're probably OK :) . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | If numeric and non-negative, go up that many more levels | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | (i.e., C<$levels==0> means only return this scope's local names). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | If C<'local'>, go up until reaching a scope with L</local> set. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | If the current scope has L</local> set, don't go up at all. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | If not provided or not defined, go all the way to the outermost Scope. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | See also L</put>, below, which is part of the public API. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | # }}}1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # Handle $levels and invoke a function on the outer scope if appropriate. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # Usage: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # $self->_invoke('method_name', $levels, [other args to be passed, starting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # with invocant, if any] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # A new levels value will be added to the end of the args as -levels=>$val. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # Returns undef if there's no more traversing to be done. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | sub _invoke { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 731 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_invoke: called CC=13 SCAR=25.7
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 731 | ▶ | my $method_name = shift or croak 'Need a method name'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 731 | my $levels = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | # Handle 'local'-scoped searches by terminating when $self->local is set. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 731 | ▶ | $levels = 0 if ( ($levels//'') eq _LOCAL) && $self->local; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
($levels // "") eq "local" and $self->local
$levels // ""
MC/DC100%2/2
($levels // "") eq "local" and $self->local
$levels
$self->local
Truth table100%3/3
A and B
A= $levelsB= $self->local
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # Search the outer scopes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 731 | ▶ | if($self->outer && # Search the outer scopes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$self->outer and !defined($levels) || $levels eq "local" || $levels > 0
not defined $levels or $levels eq "local" or $levels > 0
not defined $levels or $levels eq "local"
MC/DC100%4/4
$self->outer and !defined($levels) || $levels eq "local" || $levels > 0
$self->outer
not defined $levels
$levels eq "local"
$levels > 0
Truth table100%5/5
A and not(B) or C or D
A= $self->outerB= not defined $levelsC= $levels eq "local"D= $levels > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | (!defined($levels) || ($levels eq _LOCAL) || ($levels>0) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 544 | ▶ | my $newlevels = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | !defined($levels) ? undef : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | ( ($levels eq _LOCAL) ? _LOCAL : ($levels-1) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 544 | unshift @_, $self->outer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 544 | push @_, -levels => $newlevels; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 544 | my $coderef = $self->outer->can($method_name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 544 | ▶ | return $coderef->(@_) if $coderef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 187 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | } #_invoke() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | =head2 find | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | Find a named data item in the scope and return it. Looks up the scope chain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | to the outermost scope if necessary. Returns undef on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | failure. Usage: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | $scope->find($name[, $set[, $levels]]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | $scope->find($name[, -set => $set][, -levels => $levels]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # Alternative using named arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | Dies if given a falsy name, notably, C<'0'>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | sub find { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 544 | ▶ | my ($self, %args) = getparameters('self', [qw(name ; set levels)], @_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 544 | ▶ | croak 'Need a name' unless $args{name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | # Therefore, '0' is not a valid name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 544 | my $levels = $args{levels}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 544 | my $here = $self->_find_here($args{name}, $args{set}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 544 | ▶ | return $here if defined $here; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | return $self->_invoke('find', $args{levels}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 207 | forward_opts(\%args, {'-'=>1}, qw(name set)) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | } #find() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | =head2 names | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | Returns a L<Set::Scalar> of the names of the items available through this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | Scope, optionally including all its parent Scopes (if any). Usage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | and example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | my $set = $scope->names([$levels]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | say "Name $_ is available" foreach @$set; # Set::Scalar supports @$set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | If no names are available in the given C<$levels>, returns an empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | C<Set::Scalar>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | TODO support a C<$set> parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | sub names { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 74 | ▶ | my ($self, %args) = getparameters('self', [qw(; levels)], @_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 74 | my $retval = Set::Scalar->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 74 | $self->_fill_names($retval, $args{levels}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 74 | return $retval; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | } #names() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | # Implementation of names() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | sub _fill_names { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | #say Dumper(\@_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 191 | ▶ | my ($self, %args) = getparameters('self', [qw(retval levels)], @_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fill_names: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 191 | $self->_names_here($args{retval}); # Insert this scope's names | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 191 | return $self->_invoke('_fill_names', $args{levels}, -retval=>$args{retval}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | } #_fill_names() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | =head2 as_hashref | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | Returns a hash of the items available through this Scope, optionally | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | including all its parent Scopes (if any). Usage: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | my $hashref = $scope->as_hashref([-levels => $levels][, -deep => $deep]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | If C<$levels> is provided and nonzero, go up that many more levels | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | (i.e., C<$levels==0> means only return this scope's local names). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | If C<$levels> is not provided, go all the way to the outermost Scope. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | If C<$deep> is provided and truthy, make a deep copy of each value (using | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | L<Data::Hopen/clone>. Otherwise, just copy. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | TODO support a C<$set> parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | sub as_hashref { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 107 | ▶ | my ($self, %args) = getparameters('self', [qw(; levels deep)], @_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_hashref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 107 | my $hrRetval = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 107 | $self->_fill_hashref($hrRetval, $args{deep}, $args{levels}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 107 | return $hrRetval; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | } #as_hashref() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # Implementation of as_hashref. Mutates the provided $hrRetval. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | sub _fill_hashref { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 333 | ▶ | my ($self, %args) = getparameters('self', [qw(retval levels deep)], @_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fill_hashref: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 333 | my $hrRetval = $args{retval}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | # Innermost wins, so copy ours first. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 333 | my $names = Set::Scalar->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 333 | $self->_names_here($names); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 333 | foreach my $k (@$names) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 281 | ▶ | unless(exists($hrRetval->{$k})) { # An inner scope might have set it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 199 | my $val = $self->find($k, -levels => 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | $hrRetval->{$k} = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 199 | ▶ | ($args{deep} ? clone($val) : $val); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | return $self->_invoke('_fill_hashref', $args{levels}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 333 | forward_opts(\%args, {'-'=>1}, qw(retval deep))); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | } #_fill_hashref() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | =head2 outerize | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | Set L</outer>, and return a scalar that will restore L</outer> when it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | goes out of scope. Usage: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | my $saver = $scope->outerize($new_outer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | C<$new_outer> may be C<undef> or a valid C<Scope>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | sub outerize { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 113 | ▶ | my ($self, %args) = getparameters('self', [qw(outer)], @_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
outerize: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | croak 'Need a Scope' unless | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | (!defined($args{outer})) or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 113 | ▶ | (ref $args{outer} && eval { $args{outer}->DOES('Data::Hopen::Scope') }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
not defined $args{"outer"} or ref $args{"outer"} and eval {
do {
$args{"outer"}->DOES("Data::Hopen::Scope")
}
}
ref $args{"outer"} and eval {
do {
$args{"outer"}->DOES("Data::Hopen::Scope")
}
}
MC/DC0%0/3
not defined $args{"outer"} or ref $args{"outer"} and eval {
do {
$args{"outer"}->DOES("Data::Hopen::Scope")
}
}
not defined $args{"outer"}
ref $args{"outer"}
eval {
do {
$args{"outer"}->DOES("Data::Hopen::Scope")
}
}
Truth table50%2/4
not(A) or B and C
A= not defined $args{"outer"}B= ref $args{"outer"}C= eval {
do {
$args{"outer"}->DOES("Data::Hopen::Scope")
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | # Protect the author of this function from himself | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 113 | ▶ | croak 'Sorry, but I must insist that you save my return value' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | unless defined wantarray; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 113 | my $old_outer = $self->outer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 113 | ▶ | my $saver = scope_finalizer { $self->outer($old_outer) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 113 | $self->outer($args{outer}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 113 | return $saver; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | } #outerize() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | =head2 _merger (internal) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | Creates a L<Hash::Merge> instance based on L</merge_strategy>, if one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | doesn't exist. Returns the instance. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | Provided for the convenience of subclasses; not actually used by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | any concrete functions in this package. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | sub _merger { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 39 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_merger: called CC=8 SCAR=21.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 39 | ▶ | return $self->_merger_instance if $self->_merger_instance; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 28 | my $s = $self->merge_strategy; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 28 | ▶ | my $precedence = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | !defined $s ? 'RETAINMENT_PRECEDENT' : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | $s =~ /^combine$/i ? 'RETAINMENT_PRECEDENT' : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | $s =~ /^keep$/i ? 'LEFT_PRECEDENT' : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | $s =~ /^replace$/i ? 'RIGHT_PRECEDENT' : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 28 | ▶ | die "Invalid merge strategy $s" unless defined $precedence; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 28 | my $merger = Hash::Merge->new($precedence); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 28 | $merger->set_clone_behavior(false); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | # TODO CHECKME --- I would rather clone everything except blessed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | # references, but doing so appears to be nontrivial. For now, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | # I am trying not cloning. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 28 | $self->_merger_instance($merger); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 28 | return $merger; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } #_merger() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | =head1 FUNCTIONS TO BE OVERRIDDEN IN SUBCLASSES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | To implement a Scope with a different data-storage model than the hash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | this class uses, subclass Scope and override these functions. Of these, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | only L</put> and L</merge> are part of the public API. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | =head2 put | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | Add key-value pairs to this scope. Returns the scope so you can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | chain. Example usage: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | my $scope = Data::Hopen::Scope->new()->put(foo => 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | C<put> overwrites data in case of any conflicts. See L</merge> if you | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | want more control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | C<put> may be called with no parameters, in which case it is a no-op. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | This is so you can say C<< $s->put(%foo) >> without first having to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | check whether C<%foo> is nonempty. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | TODO add C<$set> option. TODO? add -deep option? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | sub put { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | ▶ | } #put() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
put: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | =head2 merge | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | Merges key-value pairs into this scope. Returns the scope so you can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | chain. Example usage: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | my $scope = Data::Hopen::Scope->new()->merge(foo => 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | See L</merge_strategy> for options controlling the behaviour of C<merge()>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | sub merge { #blub blub | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
merge: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | my $merger = $self->_merger; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | } #merge() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | =head2 _names_here | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | Populates a L<Set::Scalar> with the names of the items stored in this Scope, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | but B<not> any outer Scope. Called as: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | $scope->_names_here($retval[, $set]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | C<$retval> is the C<Set::Scalar> instance. C<$set> is as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | defined L<above|/$set>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | No return value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | sub _names_here { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | } #_names_here() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_names_here: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | =head2 _find_here | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | Looks for a given item in this scope, but B<not> any outer scope. Called as: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | $scope->_find_here($name[, $set]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | Returns the value, or C<undef> if not found. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | sub _find_here { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | ▶ | } #_find_here() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_here: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | =head1 HELPER FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | =head2 is_first_only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | Test whether the given scalar is L</FIRST_ONLY>. Usage: C<is_first_only($x)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | sub is_first_only { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 1078 | ▶ | ref $_[0] && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_first_only: called CC=3 SCAR=11.0
Pod
documented
Branch: 1 branch
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_first_only
MC/DC100%2/2
ref $_[0] and ref $_[0] eq ref $_first_only
ref $_[0]
ref $_[0] eq ref $_first_only
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_first_only
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | ref $_[0] eq ref $_first_only && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | refaddr $_[0] == refaddr $_first_only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | } #is_first_only() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | =head2 _set0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | For use only by subclasses. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | Don't support C<-set>, but permit C<< -set=>0 >> and C<< -set=>FIRST_ONLY >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | for the sake of code calling through the Scope interface. Call as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | C<set0($set)>>. Returns truthy if OK, falsy if not. May modify its argument. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | Better a readily-obvious crash than a subtle bug! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | sub _set0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 1073 | ▶ | $_[0] //= 0; # Give the caller a default set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set0: called CC=5 SCAR=16.1
Condition100%2/2
$_[0] //= 0
MC/DC100%1/1
$_[0] //= 0
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 1073 | ▶ | $_[0] = 0 if Data::Hopen::Scope::is_first_only($_[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 1073 | my $set = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 1073 | ▶ | return false if $set ne '0' && $set ne '*'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$set ne 0 and $set ne "*"
MC/DC100%2/2
$set ne 0 and $set ne "*"
$set ne 0
$set ne "*"
Truth table100%3/3
A and B
A= $set ne 0B= $set ne "*"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 1071 | return true; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | } #_set0() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 18 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||