| 1 | package App::SimpleScan::Substitution; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 42 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 42 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 42 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 42 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 42 | our $VERSION = '1.00'; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 42 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 42 | ▶ | use App::SimpleScan::TestSpec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 42 | ▶ | use App::SimpleScan::Substitution::Line; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 42 | ▶ | use Graph; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 42 | ▶ | use base qw(Class::Accessor::Fast); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 42 | __PACKAGE__->mk_accessors( qw(dictionary find_vars_callback | |||||||||||||||||||
| 17 | insert_value_callback) ); | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # Define base variable detector: any (possibly-nested) angle-bracketed string. | ||||||||||||||||||||
| 20 | # Patterns to extract <variables> or >variables< from a string. | ||||||||||||||||||||
| 21 | 42 | my $out_angled; | |||||||||||||||||||
| 22 | 42 | $out_angled = qr/ < ( [^<>] | (??{$out_angled}) )* > /x; | |||||||||||||||||||
| 23 | # open angle-bracket then ... | ||||||||||||||||||||
| 24 | # non-angle chars ... | ||||||||||||||||||||
| 25 | # or ... | ||||||||||||||||||||
| 26 | # another angle-bracketed item ... | ||||||||||||||||||||
| 27 | # if there are any ... | ||||||||||||||||||||
| 28 | # and a close angle-bracket | ||||||||||||||||||||
| 29 | 42 | my $in_angled; | |||||||||||||||||||
| 30 | 42 | $in_angled = qr/ > ( [^<>] | (??{$in_angled}) )* < /x; | |||||||||||||||||||
| 31 | # open angle-bracket then ... | ||||||||||||||||||||
| 32 | # non-angle chars ... | ||||||||||||||||||||
| 33 | # or ... | ||||||||||||||||||||
| 34 | # another angle-bracketed item ... | ||||||||||||||||||||
| 35 | # if there are any ... | ||||||||||||||||||||
| 36 | # and a close angle-bracket | ||||||||||||||||||||
| 37 | 42 | my $in_or_out_bracketed = qr/ ($out_angled) | ($in_angled) /x; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 453 | ▶ | sub _find_angle_bracketed { | ||||||||||||||||||
|
Subroutine
_find_angle_bracketed: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 453 | my ($string) = @_; | |||||||||||||||||||
| 41 | 453 | local $_; | |||||||||||||||||||
| 42 | 453 | my @artifacted = ( $string =~ /$in_or_out_bracketed/xg ); # match bracketed+artifacts | |||||||||||||||||||
| 43 | 453 | my @defed = grep { defined $_ } @artifacted; | |||||||||||||||||||
| 44 | 453 | my @angled = grep { ($_) = /^[<>](.*?)[<>]$/ } @defed; | |||||||||||||||||||
| 45 | 453 | return @angled; | |||||||||||||||||||
| 46 | #return grep { ($_) = /^[<>](.*?)[<>]$/ } # true angle-bracketed text | ||||||||||||||||||||
| 47 | # grep { defined $_ } # can include undef items | ||||||||||||||||||||
| 48 | # ( $string =~ /($angle_bracketed)/xg ); # match bracketed+artifacts | ||||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 200 | ▶ | sub _insert_value { | ||||||||||||||||||
|
Subroutine
_insert_value: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 52 | 200 | my ($string, $variable, $value) = @_; | |||||||||||||||||||
| 53 | 200 | my $was_inserted; | |||||||||||||||||||
| 54 | 200 | ▶ | $was_inserted ||= ($string =~ s/<$variable>/$value/gs); | ||||||||||||||||||
|
Condition66%2/3
$was_inserted ||= $string =~ s/<$variable>/$value/gs
MC/DC50%1/2
$was_inserted ||= $string =~ s/<$variable>/$value/gs
$was_inserted
$string =~ s/<$variable>/$value/gs
Truth table66%2/3
A ||= B
A= $was_insertedB= $string =~ s/<$variable>/$value/gs
| |||||||||||||||||||||
| 55 | 200 | ▶ | $was_inserted ||= ($string =~ s/>$variable</$value/gs); | ||||||||||||||||||
|
Condition100%3/3
$was_inserted ||= $string =~ s/>$variable</$value/gs
MC/DC100%2/2
$was_inserted ||= $string =~ s/>$variable</$value/gs
$was_inserted
$string =~ s/>$variable</$value/gs
Truth table100%3/3
A ||= B
A= $was_insertedB= $string =~ s/>$variable</$value/gs
| |||||||||||||||||||||
| 56 | 200 | ($was_inserted, $string); | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | ################################ | ||||||||||||||||||||
| 61 | # Basic class methods. | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | # Create object: | ||||||||||||||||||||
| 64 | # empty dictionary, default variable definition. | ||||||||||||||||||||
| 65 | # Update with values from the arg ref if they're there. | ||||||||||||||||||||
| 66 | 48 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||
| 67 | 48 | my ($class, $arg_ref) = @_; | |||||||||||||||||||
| 68 | 48 | my $self = {}; | |||||||||||||||||||
| 69 | 48 | bless $self, $class; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 48 | $self->dictionary({}); | |||||||||||||||||||
| 72 | 48 | $self->find_vars_callback(\&_find_angle_bracketed); | |||||||||||||||||||
| 73 | 48 | $self->insert_value_callback(\&_insert_value); | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 48 | ▶ | if ($arg_ref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 6 | ▶ | if (ref $arg_ref eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 4 | ▶ | if (exists $arg_ref->{dictionary}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 2 | ▶ | if (ref $arg_ref->{dictionary} eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 1 | $self->dictionary($arg_ref->{dictionary}); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||
| 82 | 1 | croak "'dictionary' must be a hash reference"; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | 3 | ▶ | if (exists $arg_ref->{find_vars_callback}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 2 | ▶ | if (ref $arg_ref->{find_vars_callback} eq 'CODE') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | 1 | $self->dictionary($arg_ref->{find_vars_callback}); | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||
| 90 | 1 | croak "'find_vars_callback' must be a code reference"; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||
| 95 | 2 | croak "Argument to new is not a hash reference"; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 44 | return $self; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | # We look at the current line and find substitution which is most deeply nested. | ||||||||||||||||||||
| 103 | 211 | ▶ | sub _deepest_substitution { | ||||||||||||||||||
|
Subroutine
_deepest_substitution: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 104 | 211 | my($self, $string) = @_; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | # We absolutely have to localize $_ because we may get called recursively | ||||||||||||||||||||
| 107 | # inside the map() below. | ||||||||||||||||||||
| 108 | 211 | local $_; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # Get the variables in this line. We'll need these and all their | ||||||||||||||||||||
| 111 | # dependencies, but nothing else. This speeds up substitution in | ||||||||||||||||||||
| 112 | # a major way because we don't try fruitless variations of variables | ||||||||||||||||||||
| 113 | # that are not (and can't be) substituted into the line. | ||||||||||||||||||||
| 114 | # | ||||||||||||||||||||
| 115 | # First, parse the unique substitutions out of the line. | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | # Extract text corresponding to variables to be substituted. | ||||||||||||||||||||
| 118 | 211 | my @vars = $self->find_vars_callback()->($string); | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | # There seem to be none. | ||||||||||||||||||||
| 121 | 211 | ▶ | return unless @vars; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | # It's possible that a variable was mentioned multiple times in a line. | ||||||||||||||||||||
| 124 | # Extract only the unique names. | ||||||||||||||||||||
| 125 | 83 | my %unique_vars = map {$_ => 1} @vars; | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | # We only want the simple variables now. | ||||||||||||||||||||
| 128 | 83 | @vars = keys %unique_vars; | |||||||||||||||||||
| 129 | 83 | local $_; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 83 | my %finished; | |||||||||||||||||||
| 132 | 83 | ▶ | while (@vars) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 143 | my $current = shift @vars; | |||||||||||||||||||
| 134 | 143 | my @deeper = $self->_deepest_substitution($current); | |||||||||||||||||||
| 135 | 143 | ▶ | if (@deeper) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 15 | push @vars, @deeper; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | else { | ||||||||||||||||||||
| 139 | 128 | $finished{$current}++; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | 83 | return keys %finished; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | # If the current thing has substitutions in it, do them and | ||||||||||||||||||||
| 146 | # return the line objects created by this. Otherwise, just | ||||||||||||||||||||
| 147 | # return the lines as they are. | ||||||||||||||||||||
| 148 | 121 | ▶ | sub expand { | ||||||||||||||||||
|
Subroutine
expand: called CC=5 SCAR=19.1
Pod
documented
| |||||||||||||||||||||
| 149 | 121 | my($self, @lines) = @_; | |||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | # No lines; do nothing. | ||||||||||||||||||||
| 152 | 121 | ▶ | return () if @lines == 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | # A single line; find the variables in it and expand them. | ||||||||||||||||||||
| 155 | 121 | ▶ | if (@lines == 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | # Nothing to do if no vars found. | ||||||||||||||||||||
| 157 | 121 | ▶ | return $lines[0] unless $self->find_vars_callback->($lines[0]); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 29 | return map {"$_" } $self->_expand_variables( | |||||||||||||||||||
| 160 | App::SimpleScan::Substitution::Line->new($lines[0]) | ||||||||||||||||||||
| 161 | ); | ||||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | # Multiple lines; do them one at a time and return the results of | ||||||||||||||||||||
| 164 | # doing them all. | ||||||||||||||||||||
| 165 | else { | ||||||||||||||||||||
| 166 | 0 | my @done; | |||||||||||||||||||
| 167 | 0 | foreach my $line (@lines) { | |||||||||||||||||||
| 168 | 0 | push @done, $self->_expand($line); | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | 0 | return map { "$_" } @done; | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | # Actually do variable substitutions. | ||||||||||||||||||||
| 175 | 69 | ▶ | sub _expand_variables { | ||||||||||||||||||
|
Subroutine
_expand_variables: called CC=10 SCAR=23.0
| |||||||||||||||||||||
| 176 | # We get a Line object. This object has its own "fixed" dictionary | ||||||||||||||||||||
| 177 | # associated with it: this defines the variables that have already | ||||||||||||||||||||
| 178 | # been expanded once for this Line. We have this because it's possible | ||||||||||||||||||||
| 179 | # that a later expansion may insert one of these variables into the | ||||||||||||||||||||
| 180 | # line again, and we want to have a consistent value for the variable(s) | ||||||||||||||||||||
| 181 | # we've already inserted once. | ||||||||||||||||||||
| 182 | 69 | my ($self, @line_objs) = @_; | |||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | # No objects, no output. | ||||||||||||||||||||
| 185 | 69 | ▶ | return unless @line_objs; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | # More than one: process each one separately. | ||||||||||||||||||||
| 188 | 69 | ▶ | return map { $self->_expand_variables($_) } @line_objs | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | if @line_objs > 1; | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | # A single line object; process it. | ||||||||||||||||||||
| 192 | 64 | my $line_obj = $line_objs[0]; | |||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | # Clone the dictionary, because we're going to modify it with the | ||||||||||||||||||||
| 195 | # fixed values. This effectively prunes the substitution tree at the | ||||||||||||||||||||
| 196 | # points where we've already done substitutions. | ||||||||||||||||||||
| 197 | 64 | my %dictionary = (%{ $self->dictionary() }, $line_obj->fixed); | |||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | # Localize the slot that contains the dictionary and replace it | ||||||||||||||||||||
| 200 | # with our newly-constructed, possibly-pruned one. | ||||||||||||||||||||
| 201 | 64 | local($self->{dictionary}) = \%dictionary; | |||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | # Find the most-deeply-nested substitutions; we need to do those first; | ||||||||||||||||||||
| 204 | # prune out anything that looks like a variable, but isn't (because | ||||||||||||||||||||
| 205 | # there's no value for it in the dictionary). | ||||||||||||||||||||
| 206 | 64 | my @var_names = grep { defined $self->dictionary->{$_} } | |||||||||||||||||||
| 207 | $self->_deepest_substitution("$line_obj"); | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | # We have none. | ||||||||||||||||||||
| 210 | 64 | ▶ | return $line_obj unless @var_names; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | # What we want to do is to get every possible combination of the | ||||||||||||||||||||
| 213 | # active variables in this line from the dictionary, and substitute | ||||||||||||||||||||
| 214 | # all these into the line. | ||||||||||||||||||||
| 215 | # | ||||||||||||||||||||
| 216 | # Since we have a situation where we don't know how many variables there | ||||||||||||||||||||
| 217 | # are, we can't just code this as a set of nested loops. What we do instead | ||||||||||||||||||||
| 218 | # is map each possible combination into a "combination index": think of it | ||||||||||||||||||||
| 219 | # as the decimal representation of a number in a number system where each | ||||||||||||||||||||
| 220 | # position in this system's representation maps into a specific variable. | ||||||||||||||||||||
| 221 | # The number of possible values for this "place" in the number corresponds | ||||||||||||||||||||
| 222 | # to the number of possible values for thr variable. | ||||||||||||||||||||
| 223 | # | ||||||||||||||||||||
| 224 | # It's easy for us to calculate the number of possible combinations: we | ||||||||||||||||||||
| 225 | # simply multiply the number of possible values of all of the variables, | ||||||||||||||||||||
| 226 | # and we get the maximum possible combination index. We can now iterate | ||||||||||||||||||||
| 227 | # from zero to this maxiumum index, converting the decimal number back into | ||||||||||||||||||||
| 228 | # a number in the combinatorial number system; the representation we get | ||||||||||||||||||||
| 229 | # from doing this exactly maps into the proper indexes into the possible | ||||||||||||||||||||
| 230 | # values for each variable. | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | # Count the number of items for each substitution, | ||||||||||||||||||||
| 233 | # and calculate the maximum combination index from this. | ||||||||||||||||||||
| 234 | 62 | my %item_count_for; | |||||||||||||||||||
| 235 | 62 | my $max_combination = 1; | |||||||||||||||||||
| 236 | 62 | for my $var_name (sort @var_names) { | |||||||||||||||||||
| 237 | $max_combination *= | ||||||||||||||||||||
| 238 | 91 | $item_count_for{$var_name} = () = | |||||||||||||||||||
| 239 | $self->substitution_value($var_name); | ||||||||||||||||||||
| 240 | } | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | # The done queue gets Line objects that don't expand further; | ||||||||||||||||||||
| 243 | # the expansion queue gets things that expanded at least once | ||||||||||||||||||||
| 244 | # (so they need to be checked again). | ||||||||||||||||||||
| 245 | 62 | my @done_queue; | |||||||||||||||||||
| 246 | my @expansion_queue; | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | 62 | for my $i (0 .. $max_combination-1) { | |||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | # Get the values for the variables for the current combination index. | ||||||||||||||||||||
| 251 | 118 | my %current_value_of = $self->_comb_index($i, %item_count_for); | |||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | # Clone the current line. This keeps the fixed items and copies | ||||||||||||||||||||
| 254 | # the line text. | ||||||||||||||||||||
| 255 | 118 | my $changed_line = $line_obj->clone(); | |||||||||||||||||||
| 256 | 118 | my $string_to_alter = $changed_line->line(); | |||||||||||||||||||
| 257 | 118 | my $a_substitution_happened; | |||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | # Try to substitute each of the currently variables into the line. | ||||||||||||||||||||
| 260 | 118 | for my $var_name (@var_names) { | |||||||||||||||||||
| 261 | 198 | my $current_variable_value = $current_value_of{$var_name}; | |||||||||||||||||||
| 262 | 198 | my($did_change, $new_string) = | |||||||||||||||||||
| 263 | $self->insert_value_callback->($string_to_alter, | ||||||||||||||||||||
| 264 | $var_name, | ||||||||||||||||||||
| 265 | $current_variable_value); | ||||||||||||||||||||
| 266 | 198 | ▶ | if ($did_change) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 267 | # Substitution worked. Fix this in the new line object. | ||||||||||||||||||||
| 268 | 198 | $changed_line->fix($var_name, [$current_variable_value]); | |||||||||||||||||||
| 269 | 198 | $changed_line->line( $string_to_alter = $new_string ); | |||||||||||||||||||
| 270 | } | ||||||||||||||||||||
| 271 | } | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | # Decide which queue to put this object on and put it there. | ||||||||||||||||||||
| 275 | 118 | ▶ | my $proper_queue = ($self->find_vars_callback->($string_to_alter) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 276 | ? \@expansion_queue | ||||||||||||||||||||
| 277 | : \@done_queue | ||||||||||||||||||||
| 278 | ); | ||||||||||||||||||||
| 279 | 118 | push @{ $proper_queue }, $changed_line; | |||||||||||||||||||
| 280 | } | ||||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | 62 | ▶ | if (@expansion_queue) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 283 | 22 | return @done_queue, $self->_expand_variables(@expansion_queue); | |||||||||||||||||||
| 284 | } | ||||||||||||||||||||
| 285 | else { | ||||||||||||||||||||
| 286 | 40 | return @done_queue; | |||||||||||||||||||
| 287 | } | ||||||||||||||||||||
| 288 | } | ||||||||||||||||||||
| 289 | |||||||||||||||||||||
| 290 | 142 | ▶ | sub _comb_index { | ||||||||||||||||||
|
Subroutine
_comb_index: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 291 | # this subroutine converts a combination index to a specific set of | ||||||||||||||||||||
| 292 | # values, one for each of the variables in the list. | ||||||||||||||||||||
| 293 | 142 | my($self, $index, %item_counts) = @_; | |||||||||||||||||||
| 294 | 142 | my @indexes = $self->_comb($index, %item_counts); | |||||||||||||||||||
| 295 | 142 | my $i = 0; | |||||||||||||||||||
| 296 | 142 | my %selection_for; | |||||||||||||||||||
| 297 | 142 | my @ordered_keys = sort keys %item_counts; | |||||||||||||||||||
| 298 | 142 | local $_; ##no critic | |||||||||||||||||||
| 299 | 142 | my %base_map_of = map { $_ => $i++ } @ordered_keys; | |||||||||||||||||||
| 300 | 142 | for my $var (@ordered_keys) { | |||||||||||||||||||
| 301 | 294 | my $value_ref = $self->substitution_value($var); | |||||||||||||||||||
| 302 | 294 | ▶ | if (defined $value_ref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 303 | $selection_for{$var} = | ||||||||||||||||||||
| 304 | 294 | $self->substitution_value($var)->[$indexes[$base_map_of{$var}]]; | |||||||||||||||||||
| 305 | } | ||||||||||||||||||||
| 306 | else { | ||||||||||||||||||||
| 307 | 0 | $selection_for{$var} = undef; | |||||||||||||||||||
| 308 | } | ||||||||||||||||||||
| 309 | } | ||||||||||||||||||||
| 310 | 142 | return %selection_for; | |||||||||||||||||||
| 311 | } | ||||||||||||||||||||
| 312 | |||||||||||||||||||||
| 313 | 166 | ▶ | sub _comb { | ||||||||||||||||||
|
Subroutine
_comb: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 314 | # Convert a combination index into a list of indexes into the | ||||||||||||||||||||
| 315 | # value arrays. We don't try to look up tha values, just calculate | ||||||||||||||||||||
| 316 | # the indexes. | ||||||||||||||||||||
| 317 | 166 | my($self, $index, %item_counts) = @_; | |||||||||||||||||||
| 318 | 166 | my @base_order = sort keys %item_counts; | |||||||||||||||||||
| 319 | 166 | my @comb; | |||||||||||||||||||
| 320 | 166 | my $place = 0; | |||||||||||||||||||
| 321 | |||||||||||||||||||||
| 322 | # All indexes must start at zero. | ||||||||||||||||||||
| 323 | 166 | my $number_of_items = scalar keys %item_counts; | |||||||||||||||||||
| 324 | 166 | foreach my $item (keys %item_counts) { | |||||||||||||||||||
| 325 | 390 | push @comb, 0; | |||||||||||||||||||
| 326 | } | ||||||||||||||||||||
| 327 | |||||||||||||||||||||
| 328 | # convert from base 10 to the derived multi-base number | ||||||||||||||||||||
| 329 | # that maps into the indexes into the possible values. | ||||||||||||||||||||
| 330 | 166 | ▶ | while ($index) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 331 | 244 | $comb[$place] = $index % $item_counts{$base_order[$place]}; | |||||||||||||||||||
| 332 | 244 | $index = int $index/$item_counts{$base_order[$place]}; | |||||||||||||||||||
| 333 | 244 | $place++; | |||||||||||||||||||
| 334 | } | ||||||||||||||||||||
| 335 | 166 | return @comb; | |||||||||||||||||||
| 336 | } | ||||||||||||||||||||
| 337 | |||||||||||||||||||||
| 338 | # setter/getter for substitution data. | ||||||||||||||||||||
| 339 | # - setter needs a name and a list of values. | ||||||||||||||||||||
| 340 | # - getter needs a name, returns a list of values. | ||||||||||||||||||||
| 341 | 811 | ▶ | sub substitution_value { | ||||||||||||||||||
|
Subroutine
substitution_value: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||
| 342 | 811 | my ($self, $pragma_name, @pragma_values) = @_; | |||||||||||||||||||
| 343 | 811 | ▶ | if (! defined $pragma_name) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 344 | 0 | die 'No pragma specified'; | |||||||||||||||||||
| 345 | } | ||||||||||||||||||||
| 346 | 811 | ▶ | if (@pragma_values) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 347 | 124 | $self->dictionary->{$pragma_name} = \@pragma_values; | |||||||||||||||||||
| 348 | } | ||||||||||||||||||||
| 349 | return | ||||||||||||||||||||
| 350 | wantarray ? ( exists $self->dictionary->{$pragma_name} | ||||||||||||||||||||
| 351 | 91 | ? @{$self->dictionary->{$pragma_name}} | |||||||||||||||||||
| 352 | : () ) | ||||||||||||||||||||
| 353 | 811 | ▶ | : $self->dictionary->{$pragma_name}; | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 354 | } | ||||||||||||||||||||
| 355 | |||||||||||||||||||||
| 356 | 1 | ▶ | sub delete_substitution { | ||||||||||||||||||
|
Subroutine
delete_substitution: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 357 | 1 | my ($self, $substitution_name) = @_; | |||||||||||||||||||
| 358 | 1 | ▶ | return unless defined $substitution_name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 359 | 1 | delete $self->dictionary->{$substitution_name}; | |||||||||||||||||||
| 360 | 1 | return; | |||||||||||||||||||
| 361 | } | ||||||||||||||||||||
| 362 | |||||||||||||||||||||
| 363 | 42 | 1; # Magic true value required at end of module | |||||||||||||||||||
| 364 | __END__ | ||||||||||||||||||||