Porf/Util.pm

filter: Statementstmt 91.4% 86 / 94 Branchbran 80.7% 42 / 52 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 10 totaltotal 82.2% 144 / 175 CC 29 SCAR 17.2
CC
29
Cov
87%
interprete_value_options
11.0
interprete_alignment
6.5
print_hash_ref
6.0
CRAP 5.6
« Porf/Framework.pm ↑ Summary Porf/examples/minimal_hash.pl »
1 # perl
2 #
3 # Class Report::Porf::Util
4 #
5 # Utilities for the Perl Open Report Framework (Porf)
6 #
7 # Ralf Peine, Tue May 27 11:30:37 2014
8 #
9 # More documentation at the end of file
10 #------------------------------------------------------------------------------
11
12 1 $VERSION = "2.001";
13
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 #--------------------------------------------------------------------------------
18 #
19 # Report::Porf::Util;
20 #
21 #--------------------------------------------------------------------------------
22
23 package Report::Porf::Util;
24
25 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
26
27 1 ▶use base qw (Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
28
29 1 our @EXPORT = qw (
30 print_hash_ref verbose
31 get_option_value interprete_value_options complete_value_code
32 interprete_alignment const_length_left const_length_center const_length_right
33 );
34
35 # --- Print out hash content, flat ----------------------------------------------
36 0 ▶sub print_hash_ref {
Subroutine print_hash_ref: not called CC=2 SCAR=17.9
Pod undocumented
37 my (
38 0 $hash_ref, # hash to print
39 ) = @_;
40
41 0 print "#------------------------------\n";
42 0 foreach my $key (sort(keys(%$hash_ref))) {
43 0 print "$key = ".$hash_ref->{$key}."\n";
44 }
45 }
46
47 # --- true if $value greater than internal stored value ------------------------
48 1123 ▶sub verbose {
Subroutine verbose: called CC=4 SCAR=13.9
Pod undocumented
49 1123 my ($instance, # instance_ref
50 $verbose # to compare,
51 ) = @_;
52
53 1123 my $verbose_set = $instance->get_verbose();
54
55 1123 ▶ return ($verbose_set >= $verbose ? $verbose_set: 0)
Branch: 2 branches
BranchTrueFalseTotal
if defined $verbose and defined $verbose_set 1121 2 1123
$verbose_set >= $verbose ? : 1 1120 1121
Condition66%2/3
defined $verbose and defined $verbose_set
!ll&&!rl&&r
201121
MC/DC50%1/2
defined $verbose and defined $verbose_set
defined $verbose defined $verbose_set
Truth table66%2/3
A and B
A= defined $verboseB= defined $verbose_set
ABresult
0X0
100
111
56 if defined $verbose && defined $verbose_set;
57 2 return $verbose_set;
58 }
59
60 # --- get option value by different keys ---------------------------------------
61 895 ▶sub get_option_value {
Subroutine get_option_value: called CC=3 SCAR=11.0
Pod undocumented
62 895 my $option_ref = shift; # ref to option hash
63
64 895 my $key; # check all other args as key in option_ref
65
66 895 ▶ while ($key = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key = shift()) 2200 564 2764
67 2200 ▶ return $option_ref->{$key} if defined $option_ref->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $option_ref->{$key} 331 1869 2200
68 }
69
70 564 return undef;
71 }
72
73 # --- get active option 'value*' and check, that only one is used --------------
74 95 ▶sub interprete_value_options {
Subroutine interprete_value_options: called CC=11 SCAR=24.0
Pod undocumented
75 95 my $option_ref = shift;
76
77 95 my $value_other = get_option_value($option_ref, qw (-value -val -v));
78 95 my $value_indexed = get_option_value($option_ref, qw (-value_indexed -val_idx -vi));
79 95 my $value_named = get_option_value($option_ref, qw (-value_named -val_nam -vn));
80 95 my $value_object = get_option_value($option_ref, qw (-value_object -val_obj -vo));
81
82 95 my @used_opts;
83
84 95 ▶ push (@used_opts, "\$value_other => $value_other") if defined $value_other;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value_other 15 80 95
85 95 ▶ push (@used_opts, "\$value_indexed => $value_indexed") if defined $value_indexed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value_indexed 32 63 95
86 95 ▶ push (@used_opts, "\$value_named => $value_named") if defined $value_named;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value_named 45 50 95
87 95 ▶ push (@used_opts, "\$value_object => $value_object") if defined $value_object;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value_object 3 92 95
88
89 95 ▶ die "More than one value option used: ".join (", ", @used_opts)
Branch: 1 branch
BranchTrueFalseTotal
if scalar @used_opts > 1 0 95 95
90 if (scalar @used_opts > 1);
91
92 # recalc value
93 95 my $value_result = $value_other;
94
95 95 ▶ if (defined $value_indexed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_indexed) 32 63 95
96 32 ▶ die "Not an index for value array position: '$value_indexed'"
Branch: 1 branch
BranchTrueFalseTotal
if $value_indexed =~ /\D/ 0 32 32
97 if $value_indexed =~ /\D/;
98 32 $value_result = '$_[0]->['.$value_indexed.']';
99 }
100
101 95 ▶ if (defined $value_named) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_named) 45 50 95
102 45 $value_result = '$_[0]->{\''.$value_named.'\'}';
103 }
104
105 95 ▶ if (defined $value_object) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_object) 3 92 95
106 3 my $get_value_call = $value_object;
107 3 $get_value_call =~ s/\s*\(\s*\)\s*$//og;
108 3 $get_value_call =~ s/^\s+//og;
109 3 ▶ die "Not a method call for an object: '$value_object'"
Branch: 1 branch
BranchTrueFalseTotal
if $get_value_call =~ /\W/ 0 3 3
110 if $get_value_call =~ /\W/;
111 3 $value_result = '$_[0]->'.$get_value_call.'()';
112 }
113
114 95 return $value_result;
115 }
116
117 # --- complete value code --- add check for default value to code sequence, if $default_value defined -------
118 77 ▶sub complete_value_code {
Subroutine complete_value_code: called CC=2 SCAR=6.9
Pod undocumented
119 77 my $value_code_str = shift;
120 77 my $default_value = shift;
121
122 77 ▶ return "return $value_code_str" unless defined $default_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default_value 27 50 77
123
124 50 $default_value =~ s/'/\\'/og;
125
126 50 return "my \$value = $value_code_str;\n".
127 "\$value = '$default_value' if !defined \$value || \$value eq '';\n".
128 "return \$value;";
129 }
130
131 # --- get value for alignment --------------------------------------------------
132 79 ▶sub interprete_alignment {
Subroutine interprete_alignment: called CC=6 SCAR=18.7
Pod undocumented
133 79 my ($align_selection # value for alignment
134 ) = @_;
135
136 79 ▶ return '' unless $align_selection;
Branch: 1 branch
BranchTrueFalseTotal
unless $align_selection 0 79 79
137
138 79 ▶ if ( $align_selection =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align_selection =~ /^\s*$/) 0 79 79
139 0 return '';
140 }
141
142 79 ▶ if ($align_selection =~ /^\s*-?(l|left)\s*$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align_selection =~ /^\s*-?(l|left)\s*$/i) 56 23 79
143 56 return 'Left';
144 }
145
146 23 ▶ if ($align_selection =~ /^\s*-?(c|center)\s*$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align_selection =~ /^\s*-?(c|center)\s*$/i) 14 9 23
147 14 return 'Center';
148 }
149
150 9 ▶ if ($align_selection =~ /^\s*-?(r|right)\s*$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align_selection =~ /^\s*-?(r|right)\s*$/i) 9 0 9
151 9 return 'Right';
152 }
153
154 0 die "cannot interprete alignment '$align_selection'";
155 }
156
157 # --- align const length left ------------
158 284 ▶sub const_length_left {
Subroutine const_length_left: called CC=3 SCAR=11.2
Pod undocumented
159 284 my ($wanted_length,
160 $value
161 ) = @_;
162
163 284 my $l = length ($value);
164
165 284 ▶ if ( $l < $wanted_length) {
Branch: 2 branches
BranchTrueFalseTotal
if ($l < $wanted_length) { } 218 66 284
elsif ($l > $wanted_length) { } 0 66 66
166 218 $value .= ' ' x ($wanted_length - $l);
167 }
168 elsif ( $l > $wanted_length ) {
169 0 $value = substr ($value, 0, $wanted_length);
170 }
171
172 284 return $value;
173 }
174
175 # --- align const length center ------------
176 46 ▶sub const_length_center {
Subroutine const_length_center: called CC=3 SCAR=11.0
Pod undocumented
177 46 my ($wanted_length,
178 $value
179 ) = @_;
180
181 46 my $l = length ($value);
182
183 46 ▶ if ( $l < $wanted_length) {
Branch: 2 branches
BranchTrueFalseTotal
if ($l < $wanted_length) { } 10 36 46
elsif ($l > $wanted_length) { } 10 26 36
184 10 my $missing = $wanted_length - $l;
185 10 my $right = int($missing / 2);
186 10 my $left = $missing - $right;
187 10 $value = ' ' x ($left) . $value . ' ' x ($right);
188 }
189 elsif ( $l > $wanted_length ) {
190 10 $value = substr ($value, 0, $wanted_length);
191 }
192
193 46 return $value;
194 }
195
196 # --- align const length right ------------
197 2 ▶sub const_length_right {
Subroutine const_length_right: called CC=3 SCAR=12.7
Pod undocumented
198 2 my ($wanted_length,
199 $value
200 ) = @_;
201
202 2 my $l = length ($value);
203
204 2 ▶ if ( $l < $wanted_length) {
Branch: 2 branches
BranchTrueFalseTotal
if ($l < $wanted_length) { } 2 0 2
elsif ($l > $wanted_length) { } 0 0 0
205 2 $value = ' ' x ($wanted_length - $l) . $value;
206 }
207 elsif ( $l > $wanted_length ) {
208 0 $value = substr ($value, 0, $wanted_length);
209 }
210
211 2 return $value;
212 }
213
214 # --- escape html special chars ----------------------
215 21 ▶sub escape_html_special_chars {
Subroutine escape_html_special_chars: called CC=1 SCAR=0.0
Pod undocumented
216 21 my ($value) = @_;
217
218 21 $value =~ s/&/\&amp;/og;
219 21 $value =~ s/</\&lt;/og;
220 21 $value =~ s/>/\&gt;/og;
221
222 21 return $value;
223 }
224
225
226 1 1;
227
228 =head1 NAME
229
230 C<Report::Porf::Util>
231
232 Utilities for Perl Open Report Framework (Porf).
233
234 =head1 Documentation
235
236 See Framework.pm for documentation of features and usage.
237
238 =head1 LICENSE AND COPYRIGHT
239
240 Copyright (c) 2013 by Ralf Peine, Germany. All rights reserved.
241
242 This library is free software; you can redistribute it and/or modify
243 it under the same terms as Perl itself, either Perl version 5.6.0 or,
244 at your option, any later version of Perl 5 you may have available.
245
246 =head1 DISCLAIMER OF WARRANTY
247
248 This library is distributed in the hope that it will be useful,
249 but without any warranty; without even the implied warranty of
250 merchantability or fitness for a particular purpose.
251
252 =cut
« Porf/Framework.pm ↑ Summary Porf/examples/minimal_hash.pl »