| 1 | # This module provides various helper function used throughout the program. | ||||||||||||
| 2 | |||||||||||||
| 3 | package App::PTP::Util; | ||||||||||||
| 4 | |||||||||||||
| 5 | 2 | ▶ | use 5.022; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 2 | our $VERSION = '0.01'; | |||||||||||
| 10 | |||||||||||||
| 11 | # More or less the same as List::Util::uniqstr (or List::Util::uniq). Provided | ||||||||||||
| 12 | # here because the List::Util function is not available in 5.22 by default. | ||||||||||||
| 13 | 0 | ▶ | sub uniqstr { | ||||||||||
|
Subroutine
uniqstr: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 14 | 0 | my ($content, $markers) = @_; | |||||||||||
| 15 | 0 | for my $i (0 .. $#$content - 1) { | |||||||||||
| 16 | 0 | ▶ | if ($content->[$i] eq $content->[$i + 1]) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 17 | 0 | undef $content->[$i]; | |||||||||||
| 18 | 0 | undef $markers->[$i]; | |||||||||||
| 19 | } | ||||||||||||
| 20 | } | ||||||||||||
| 21 | 0 | @$content = grep { defined } @$content; | |||||||||||
| 22 | 0 | @$markers = grep { defined } @$markers; | |||||||||||
| 23 | } | ||||||||||||
| 24 | |||||||||||||
| 25 | # Globally delete duplicate lines even if they are not contiguous. Keep the | ||||||||||||
| 26 | # first occurrence of each string. | ||||||||||||
| 27 | 0 | ▶ | sub globaluniqstr { | ||||||||||
|
Subroutine
globaluniqstr: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 28 | 0 | my ($content, $markers) = @_; | |||||||||||
| 29 | 0 | my %seen; | |||||||||||
| 30 | 0 | for my $i (0 .. $#$content) { | |||||||||||
| 31 | 0 | ▶ | if ($seen{$content->[$i]}++) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 0 | undef $content->[$i]; | |||||||||||
| 33 | 0 | undef $markers->[$i]; | |||||||||||
| 34 | } | ||||||||||||
| 35 | } | ||||||||||||
| 36 | 0 | @$content = grep { defined } @$content; | |||||||||||
| 37 | 0 | @$markers = grep { defined } @$markers; | |||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | { | ||||||||||||
| 41 | # A simple way to make a scalar be read-only. | ||||||||||||
| 42 | package App::PTP::Util::ReadOnlyVar; | ||||||||||||
| 43 | |||||||||||||
| 44 | 10 | ▶ | sub TIESCALAR { | ||||||||||
|
Subroutine
TIESCALAR: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | 10 | my ($class, $value) = @_; | |||||||||||
| 46 | 10 | return bless \$value, $class; | |||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | 0 | ▶ | sub FETCH { | ||||||||||
|
Subroutine
FETCH: not called CC=1 SCAR=6.9
| |||||||||||||
| 50 | 0 | my ($self) = @_; | |||||||||||
| 51 | 0 | return $$self; | |||||||||||
| 52 | } | ||||||||||||
| 53 | # Does nothing. We could warn_or_die, but it does not play well with the fact | ||||||||||||
| 54 | # that we are inside the safe. | ||||||||||||
| 55 | 0 | ▶ | sub STORE { } | ||||||||||
|
Subroutine
STORE: not called CC=1 SCAR=0.0
| |||||||||||||
| 56 | # Secret hidden methods for our usage only. These methods can't be used | ||||||||||||
| 57 | # through the tie-ed variable, but only through the object returned by the | ||||||||||||
| 58 | # call to tie. | ||||||||||||
| 59 | 0 | ▶ | sub set { ## no critic (ProhibitAmbiguousNames) | ||||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
| |||||||||||||
| 60 | 0 | my ($self, $value) = @_; | |||||||||||
| 61 | 0 | $$self = $value; | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | 0 | ▶ | sub get { | ||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
| |||||||||||||
| 65 | 0 | my ($self, $value) = @_; | |||||||||||
| 66 | 0 | return $$self; | |||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 0 | ▶ | sub inc { | ||||||||||
|
Subroutine
inc: not called CC=1 SCAR=6.9
| |||||||||||||
| 70 | 0 | my ($self) = @_; | |||||||||||
| 71 | 0 | ++$$self; | |||||||||||
| 72 | } | ||||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | { | ||||||||||||
| 76 | # A simple way to make a scalar be an alias of another one (but does not allow | ||||||||||||
| 77 | # to store undef in the variable as this is used to mark non-existent lines | ||||||||||||
| 78 | # in some function and this package is tied to the marker variable). | ||||||||||||
| 79 | 2 | package App::PTP::Util::AliasVar; | |||||||||||
| 80 | |||||||||||||
| 81 | 2 | ▶ | sub TIESCALAR { | ||||||||||
|
Subroutine
TIESCALAR: called CC=1 SCAR=0.0
| |||||||||||||
| 82 | 2 | my ($class) = @_; | |||||||||||
| 83 | 2 | my $var; | |||||||||||
| 84 | 2 | return bless \$var, $class; | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | 0 | ▶ | sub FETCH { | ||||||||||
|
Subroutine
FETCH: not called CC=1 SCAR=6.9
| |||||||||||||
| 88 | 0 | my ($self) = @_; | |||||||||||
| 89 | 0 | return $$$self; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | 0 | ▶ | sub STORE { | ||||||||||
|
Subroutine
STORE: not called CC=2 SCAR=17.9
| |||||||||||||
| 93 | 0 | my ($self, $value) = @_; | |||||||||||
| 94 | # This empty string is also the value that is set in do_perl when the marker | ||||||||||||
| 95 | # is undef (this is not the same as the default 0 that is put when the array | ||||||||||||
| 96 | # is built). | ||||||||||||
| 97 | 0 | ▶ | $$$self = $value // ''; | ||||||||||
|
Condition0%0/2
$value // ""
MC/DC0%0/1
$value // ""
$value
Truth table0%0/2
A
A= $value
| |||||||||||||
| 98 | } | ||||||||||||
| 99 | # Secret hidden methods for our usage only. These methods can't be used | ||||||||||||
| 100 | # through the tie-ed variable, but only through the object returned by the | ||||||||||||
| 101 | # call to tie. | ||||||||||||
| 102 | 0 | ▶ | sub set { ## no critic (ProhibitAmbiguousNames) | ||||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
| |||||||||||||
| 103 | 0 | my ($self, $ref) = @_; | |||||||||||
| 104 | 0 | $$self = $ref; | |||||||||||
| 105 | } | ||||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | { | ||||||||||||
| 109 | # A fake array that returns the marker at a given offset from the current | ||||||||||||
| 110 | # line (and refuses to store undef in the array). | ||||||||||||
| 111 | 2 | package App::PTP::Util::MarkersArray; | |||||||||||
| 112 | 2 | our $NEGATIVE_INDICES = 1; | |||||||||||
| 113 | |||||||||||||
| 114 | 2 | ▶ | sub TIEARRAY { | ||||||||||
|
Subroutine
TIEARRAY: called CC=1 SCAR=0.0
| |||||||||||||
| 115 | 2 | my ($class, $markers, $n) = @_; | |||||||||||
| 116 | 2 | my $this = [$markers, $n]; | |||||||||||
| 117 | 2 | return bless $this, $class; | |||||||||||
| 118 | } | ||||||||||||
| 119 | |||||||||||||
| 120 | 0 | ▶ | sub FETCH { | ||||||||||
|
Subroutine
FETCH: not called CC=3 SCAR=24.8
| |||||||||||||
| 121 | 0 | my ($self, $offset) = @_; | |||||||||||
| 122 | 0 | my $index = ($$self->[1] - 1 + $offset); | |||||||||||
| 123 | 0 | ▶ | return 0 if $index < 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 124 | 0 | ▶ | return 0 if $index >= $self->FETCHSIZE(); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | 0 | return $self->[0][$index]; | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | 0 | ▶ | sub FETCHSIZE { | ||||||||||
|
Subroutine
FETCHSIZE: not called CC=1 SCAR=6.9
| |||||||||||||
| 129 | 0 | my ($self) = @_; | |||||||||||
| 130 | 0 | return scalar(@App::PTP::Commands::markers); | |||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | 0 | ▶ | sub STORE { | ||||||||||
|
Subroutine
STORE: not called CC=4 SCAR=30.0
| |||||||||||||
| 134 | 0 | my ($self, $offset, $value) = @_; | |||||||||||
| 135 | 0 | my $index = ($$self->[1] - 1 + $offset); | |||||||||||
| 136 | 0 | ▶ | return $value if $index < 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | 0 | ▶ | return $value if $index >= $self->FETCHSIZE(); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 138 | 0 | ▶ | $self->[0][$index] = $value // ''; # make it more difficult to store undef | ||||||||||
|
Condition0%0/2
$value // ""
MC/DC0%0/1
$value // ""
$value
Truth table0%0/2
A
A= $value
| |||||||||||||
| 139 | } | ||||||||||||
| 140 | 0 | ▶ | sub STORESIZE { } | ||||||||||
|
Subroutine
STORESIZE: not called CC=1 SCAR=0.0
| |||||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | 2 | 1; | |||||||||||