File Coverage

blib/lib/Report/Porf/Util.pm
Criterion Covered Total %
statement 83 91 91.2
branch 40 50 80.0
condition 2 3 66.6
subroutine 13 14 92.8
pod 0 10 0.0
total 138 168 82.1


line stmt bran cond sub pod time code
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             $VERSION = "2.001";
13            
14 1     1   10 use strict;
  1         1  
  1         44  
15 1     1   14 use warnings;
  1         2  
  1         49  
16            
17             #--------------------------------------------------------------------------------
18             #
19             # Report::Porf::Util;
20             #
21             #--------------------------------------------------------------------------------
22            
23             package Report::Porf::Util;
24            
25 1     1   5 use Carp;
  1         2  
  1         78  
26            
27 1     1   7 use base qw (Exporter);
  1         2  
  1         1752  
28            
29             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             sub print_hash_ref {
37             my (
38 0     0 0 0 $hash_ref, # hash to print
39             ) = @_;
40            
41 0         0 print "#------------------------------\n";
42 0         0 foreach my $key (sort(keys(%$hash_ref))) {
43 0         0 print "$key = ".$hash_ref->{$key}."\n";
44             }
45             }
46            
47             # --- true if $value greater than internal stored value ------------------------
48             sub verbose {
49 1123     1123 0 2178 my ($instance, # instance_ref
50             $verbose # to compare,
51             ) = @_;
52            
53 1123         2890 my $verbose_set = $instance->get_verbose();
54            
55 1123 100 66     8838 return ($verbose_set >= $verbose ? $verbose_set: 0)
    100          
56             if defined $verbose && defined $verbose_set;
57 2         11 return $verbose_set;
58             }
59            
60             # --- get option value by different keys ---------------------------------------
61             sub get_option_value {
62 895     895 0 1016 my $option_ref = shift; # ref to option hash
63            
64 895         1328 my $key; # check all other args as key in option_ref
65            
66 895         2106 while ($key = shift) {
67 2200 100       8142 return $option_ref->{$key} if defined $option_ref->{$key};
68             }
69            
70 564         1018 return undef;
71             }
72            
73             # --- get active option 'value*' and check, that only one is used --------------
74             sub interprete_value_options {
75 95     95 0 570 my $option_ref = shift;
76            
77 95         173 my $value_other = get_option_value($option_ref, qw (-value -val -v));
78 95         195 my $value_indexed = get_option_value($option_ref, qw (-value_indexed -val_idx -vi));
79 95         160 my $value_named = get_option_value($option_ref, qw (-value_named -val_nam -vn));
80 95         163 my $value_object = get_option_value($option_ref, qw (-value_object -val_obj -vo));
81            
82 95         105 my @used_opts;
83            
84 95 100       248 push (@used_opts, "\$value_other => $value_other") if defined $value_other;
85 95 100       212 push (@used_opts, "\$value_indexed => $value_indexed") if defined $value_indexed;
86 95 100       484 push (@used_opts, "\$value_named => $value_named") if defined $value_named;
87 95 100       169 push (@used_opts, "\$value_object => $value_object") if defined $value_object;
88            
89 95 50       201 die "More than one value option used: ".join (", ", @used_opts)
90             if (scalar @used_opts > 1);
91            
92             # recalc value
93 95         131 my $value_result = $value_other;
94            
95 95 100       165 if (defined $value_indexed) {
96 32 50       86 die "Not an index for value array position: '$value_indexed'"
97             if $value_indexed =~ /\D/;
98 32         63 $value_result = '$_[0]->['.$value_indexed.']';
99             }
100            
101 95 100       160 if (defined $value_named) {
102 45         80 $value_result = '$_[0]->{\''.$value_named.'\'}';
103             }
104            
105 95 100       156 if (defined $value_object) {
106 3         5 my $get_value_call = $value_object;
107 3         12 $get_value_call =~ s/\s*\(\s*\)\s*$//og;
108 3         9 $get_value_call =~ s/^\s+//og;
109 3 50       8 die "Not a method call for an object: '$value_object'"
110             if $get_value_call =~ /\W/;
111 3         6 $value_result = '$_[0]->'.$get_value_call.'()';
112             }
113            
114 95         299 return $value_result;
115             }
116            
117             # --- complete value code --- add check for default value to code sequence, if $default_value defined -------
118             sub complete_value_code {
119 77     77 0 91 my $value_code_str = shift;
120 77         86 my $default_value = shift;
121            
122 77 100       213 return "return $value_code_str" unless defined $default_value;
123            
124 50         71 $default_value =~ s/'/\\'/og;
125            
126 50         189 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             sub interprete_alignment {
133 79     79 0 727 my ($align_selection # value for alignment
134             ) = @_;
135            
136 79 50       246 return '' unless $align_selection;
137            
138 79 50       534 if ( $align_selection =~ /^\s*$/) {
139 0         0 return '';
140             }
141            
142 79 100       322 if ($align_selection =~ /^\s*-?(l|left)\s*$/i ) {
143 56         159 return 'Left';
144             }
145            
146 23 100       92 if ($align_selection =~ /^\s*-?(c|center)\s*$/i ) {
147 14         53 return 'Center';
148             }
149            
150 9 50       42 if ($align_selection =~ /^\s*-?(r|right)\s*$/i ) {
151 9         35 return 'Right';
152             }
153            
154 0         0 die "cannot interprete alignment '$align_selection'";
155             }
156            
157             # --- align const length left ------------
158             sub const_length_left {
159 284     284 0 566 my ($wanted_length,
160             $value
161             ) = @_;
162            
163 284         720 my $l = length ($value);
164            
165 284 100       736 if ( $l < $wanted_length) {
    50          
166 224         411 $value .= ' ' x ($wanted_length - $l);
167             }
168             elsif ( $l > $wanted_length ) {
169 0         0 $value = substr ($value, 0, $wanted_length);
170             }
171            
172 284         1707 return $value;
173             }
174            
175             # --- align const length center ------------
176             sub const_length_center {
177 46     46 0 70 my ($wanted_length,
178             $value
179             ) = @_;
180            
181 46         148 my $l = length ($value);
182            
183 46 100       118 if ( $l < $wanted_length) {
    100          
184 10         15 my $missing = $wanted_length - $l;
185 10         28 my $right = int($missing / 2);
186 10         10 my $left = $missing - $right;
187 10         32 $value = ' ' x ($left) . $value . ' ' x ($right);
188             }
189             elsif ( $l > $wanted_length ) {
190 10         17 $value = substr ($value, 0, $wanted_length);
191             }
192            
193 46         156 return $value;
194             }
195            
196             # --- align const length right ------------
197             sub const_length_right {
198 2     2 0 4 my ($wanted_length,
199             $value
200             ) = @_;
201            
202 2         4 my $l = length ($value);
203            
204 2 50       8 if ( $l < $wanted_length) {
    0          
205 2         9 $value = ' ' x ($wanted_length - $l) . $value;
206             }
207             elsif ( $l > $wanted_length ) {
208 0         0 $value = substr ($value, 0, $wanted_length);
209             }
210            
211 2         11 return $value;
212             }
213            
214             # --- escape html special chars ----------------------
215             sub escape_html_special_chars {
216 21     21 0 41 my ($value) = @_;
217            
218 21         54 $value =~ s/&/\&/og;
219 21         32 $value =~ s/
220 21         33 $value =~ s/>/\>/og;
221            
222 21         108 return $value;
223             }
224            
225            
226             1;
227            
228             =head1 NAME
229            
230             C
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