File Coverage

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


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 15     15   294 use 5.022;
  15         51  
6 15     15   82 use strict;
  15         24  
  15         307  
7 15     15   67 use warnings;
  15         27  
  15         10106  
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 1     1 0 224 my (@l) = @_;
13 1         7 for my $i (0 .. $#l - 1) {
14 114 50       184 undef $l[$i] if $l[$i] eq $l[$i+1];
15             }
16 1         4 return grep { defined } @_;
  115         181  
17             }
18              
19             {
20             # A simple way to make a scalar be read-only.
21             package App::PTP::Util::ReadOnlyVar;
22             sub TIESCALAR {
23 75     75   144 my ($class, $value) = @_;
24 75         185 return bless \$value, $class;
25             }
26             sub FETCH {
27 108     108   2553 my ($self) = @_;
28 108         560 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 1116     1116   2010 my ($self, $value) = @_;
38 1116         2129 $$self = $value;
39             }
40             sub get {
41 0     0   0 my ($self, $value) = @_;
42 0         0 return $$self;
43             }
44             sub inc {
45 128     128   257 my ($self) = @_;
46 128         426 ++$$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 15     15   42 my ($class) = @_;
55 15         36 my $var;
56 15         40 return bless \$var, $class;
57             }
58             sub FETCH {
59 10     10   18 my ($self) = @_;
60 10         34 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 556     556   958 my ($self, $ref) = @_;
71 556         1093 $$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 15     15   69 my ($class, $markers, $n) = @_;
82 15         53 my $this = [$markers, $n];
83 15         73 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;