File Coverage

blib/lib/App/PTP/Util.pm
Criterion Covered Total %
statement 31 47 65.9
branch 2 10 20.0
condition n/a
subroutine 12 19 63.1
pod 0 1 0.0
total 45 77 58.4


line stmt bran cond sub pod time code
1             # This module provides various helper function used throughout the program.
2              
3             package App::PTP::Util;
4              
5 19     19   349 use 5.022;
  19         54  
6 19     19   84 use strict;
  19         34  
  19         358  
7 19     19   78 use warnings;
  19         27  
  19         11078  
8              
9             # More or less the same as List::Util::uniqstr (or List::Util::uniq). Provided
10             # here because the List::Util function is not available in 5.22 by default.
11             sub uniqstr (@) {
12 3     3 0 226 my (@l) = @_;
13 3         15 for my $i (0 .. $#l - 1) {
14 131 100       185 undef $l[$i] if $l[$i] eq $l[$i+1];
15             }
16 3         9 return grep { defined } @l;
  134         178  
17             }
18              
19             {
20             # A simple way to make a scalar be read-only.
21             package App::PTP::Util::ReadOnlyVar;
22             sub TIESCALAR {
23 95     95   164 my ($class, $value) = @_;
24 95         240 return bless \$value, $class;
25             }
26             sub FETCH {
27 109     109   2682 my ($self) = @_;
28 109         641 return $$self;
29             }
30             # Does nothing. We could warn_or_die, but it does not play well with the fact
31             # that we are inside the safe.
32       0     sub STORE {}
33             # Secret hidden methods for our usage only. These methods can't be used
34             # through the tie-ed variable, but only through the object returned by the
35             # call to tie.
36             sub set {
37 1185     1185   2120 my ($self, $value) = @_;
38 1185         2197 $$self = $value;
39             }
40             sub get {
41 0     0   0 my ($self, $value) = @_;
42 0         0 return $$self;
43             }
44             sub inc {
45 142     142   274 my ($self) = @_;
46 142         568 ++$$self;
47             }
48             }
49              
50             {
51             # A simple way to make a scalar be an alias of another one.
52             package App::PTP::Util::AliasVar;
53             sub TIESCALAR {
54 19     19   45 my ($class) = @_;
55 19         32 my $var;
56 19         47 return bless \$var, $class;
57             }
58             sub FETCH {
59 10     10   16 my ($self) = @_;
60 10         35 return $$$self;
61             }
62             sub STORE {
63 0     0   0 my ($self, $value) = @_;
64 0         0 $$$self = $value;
65             }
66             # Secret hidden methods for our usage only. These methods can't be used
67             # through the tie-ed variable, but only through the object returned by the
68             # call to tie.
69             sub set {
70 558     558   942 my ($self, $ref) = @_;
71 558         1052 $$self = $ref;
72             }
73             }
74              
75             {
76             # A fake array that returns the marker at a given offset from the current
77             # line.
78             package App::PTP::Util::MarkersArray;
79             our $NEGATIVE_INDICES = 1;
80             sub TIEARRAY {
81 19     19   74 my ($class, $markers, $n) = @_;
82 19         55 my $this = [$markers, $n];
83 19         77 return bless $this, $class;
84             }
85             sub FETCH {
86 0     0     my ($self, $offset) = @_;
87 0           my $index = ($$self->[1] - 1 + $offset);
88 0 0         return 0 if $index < 0;
89 0 0         return 0 if $index >= $self->FETCHSIZE();
90 0           return $self->[0][$index];
91             }
92             sub FETCHSIZE {
93 0     0     my ($self) = @_;
94 0           return scalar(@App::PTP::Commands::markers);
95             }
96             sub STORE {
97 0     0     my ($self, $offset, $value) = @_;
98 0           my $index = ($$self->[1] - 1 + $offset);
99 0 0         return $value if $index < 0;
100 0 0         return $value if $index >= $self->FETCHSIZE();
101 0           $self->[0][$index] = $value;
102             }
103       0     sub STORESIZE {}
104             }
105              
106             1;