Simple/Util.pm

filter: Statementstmt 81.1% 82 / 101 Branchbran 45.4% 20 / 44 Conditioncond 61.5% 8 / 13 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 84.2% 16 / 19 Podpod 100% 9 / 9 totaltotal 70.9% 137 / 193 CC 30 SCAR 18.2
CC
30
Cov
70%
__ANON__
20.0
DIGEST
9.2
_is_parent_object
8.1
CRAP 6.2
« Simple/Base/Examine.pm ↑ Summary Simple/Cache/ID.pm »
1 package Text::Template::Simple::Util;
2 44 $Text::Template::Simple::Util::VERSION = '0.91';
3 44 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 44 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 44 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 44 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 44 ▶use Text::Template::Simple::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 :info
9 DIGEST_MODS
10 EMPTY_STRING
11 44 );
12  
13 44 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 44 if ( UNICODE_PERL ) {
15 # older perl binmode() does not accept a second param
16 1 ▶ *binary_mode = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
17 1 my($fh, $layer) = @_;
18 1 binmode $fh, q{:} . $layer;
19 44 };
20 }
21 else {
22 0 ▶ *binary_mode = sub { binmode $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 }
24 44 our %EXPORT_TAGS = (
25 util => [qw( binary_mode DIGEST trim rtrim ltrim escape )],
26 debug => [qw( fatal DEBUG LOG L )],
27 misc => [qw( visualize_whitespace )],
28 );
29 44 our @EXPORT_OK = map { @{ $EXPORT_TAGS{$_} } } keys %EXPORT_TAGS;
30 44 $EXPORT_TAGS{all} = \@EXPORT_OK;
31 44 our @EXPORT = @EXPORT_OK;
32 }
33  
34 44 my $lang = {
35 error => {
36 q{tts.base.examine.notglob} => q{Unknown template parameter passed as %s reference! Supported types are GLOB, PATH and STRING.},
37 q{tts.base.examine.notfh} => q{This GLOB is not a filehandle},
38 q{tts.main.cdir} => q{Cache dir %s does not exist!},
39 q{tts.main.bogus_args} => q{Malformed add_args parameter! 'add_args' must be an arrayref!},
40 q{tts.main.bogus_delims} => q{Malformed delimiters parameter! 'delimiters' must be a two element arrayref!},
41 q{tts.cache.opendir} => q{Can not open cache dir (%s) for reading: %s},
42 q{tts.util.digest} => q{Can not load a digest module. Disable cache or install one of these (%s or %s). Last error was: %s},
43 q{tts.cache.dumper} => q{Can not dump in-memory cache! Your version of Data::Dumper (%s) does not implement the Deparse() method. Please upgrade this module!},
44 q{tts.cache.pformat} => q{Parameters must be in 'param => value' format},
45 q{tts.cache.incache} => q{I need an 'id' or a 'data' parameter for cache check!},
46 q{tts.main.dslen} => q{Start delimiter is smaller than 2 characters},
47 q{tts.main.delen} => q{End delimiter is smaller than 2 characters},
48 q{tts.main.dsws} => q{Start delimiter contains whitespace},
49 q{tts.main.dews} => q{End delimiter contains whitespace},
50 q{tts.main.import.invalid} => q{%s isn't a valid import parameter for %s},
51 q{tts.main.import.undef} => q{%s is not defined in %s},
52 q{tts.main.import.redefine} => q{%s is already defined in %s},
53 q{tts.main.tts.args} => q{Nothing to compile!},
54 q{tts.main.connector.args} => q{connector(): id is missing},
55 q{tts.main.connector.invalid} => q{connector(): invalid id: %s},
56 q{tts.main.init.thandler} => q{user_thandler parameter must be a CODE reference},
57 q{tts.main.init.include} => q{include_paths parameter must be a ARRAY reference},
58 q{tts.util.escape} => q{Missing the character to escape},
59 q{tts.tokenizer.new.ds} => q{Start delimiter is missing},
60 q{tts.tokenizer.new.de} => q{End delimiter is missing},
61 q{tts.tokenizer.tokenize.tmp} => q{Template string is missing},
62 q{tts.tokenizer._get_symbols.regex} => q{Regex is missing},
63 q{tts.io.validate.type} => q{No type specified},
64 q{tts.io.validate.path} => q{No path specified},
65 q{tts.io.validate.file} => q{validate(file) is not yet implemented},
66 q{tts.io.layer.fh} => q{Filehandle is absent},
67 q{tts.io.slurp.open} => q{Error opening '%s' for reading: %s},
68 q{tts.io.slurp.taint} => q{Can't untaint FH},
69 q{tts.io.hls.invalid} => q{FH is either absent or invalid},
70 q{tts.caller.stack.hash} => q{Parameters to stack() must be a HASH},
71 q{tts.caller.stack.type} => q{Unknown caller stack type: %s},
72 q{tts.caller._text_table.module} => q{Caller stack type 'text_table' requires Text::Table: %s},
73 q{tts.cache.new.parent} => q{Parent object is missing},
74 q{tts.cache.dumper.hash} => q{Parameters to dumper() must be a HASHref},
75 q{tts.cache.dumper.type} => q{Dumper type '%s' is not valid},
76 q{tts.cache.develsize.buggy} => q{Your Devel::Size version (%s) has a known bug. Upgrade Devel::Size to 0.72 or newer or do not use the size() method},
77 q{tts.cache.develsize.total} => q{Devel::Size::total_size(): %s},
78 q{tts.cache.hit.meta} => q{Can not get meta data: %s},
79 q{tts.cache.hit.cache} => q{Error loading from disk cache: %s},
80 q{tts.cache.populate.write} => q{Error writing disk-cache %s : %s},
81 q{tts.cache.populate.chmod} => q{Can not change file mode},
82 q{tts.base.compiler._compile.notmp} => q{No template specified},
83 q{tts.base.compiler._compile.param} => q{params must be an arrayref!},
84 q{tts.base.compiler._compile.opt} => q{opts must be a hashref!},
85 q{tts.base.compiler._wrap_compile.parsed} => q{nothing to compile},
86 q{tts.base.compiler._mini_compiler.notmp} => q{_mini_compiler(): missing the template},
87 q{tts.base.compiler._mini_compiler.noparam} => q{_mini_compiler(): missing the parameters},
88 q{tts.base.compiler._mini_compiler.opt} => q{_mini_compiler(): options must be a hash},
89 q{tts.base.compiler._mini_compiler.param} => q{_mini_compiler(): parameters must be a HASH},
90 q{tts.base.examine._examine_type.ftype} => q{ARRAY does not contain the type},
91 q{tts.base.examine._examine_type.fthing} => q{ARRAY does not contain the data},
92 q{tts.base.examine._examine_type.extra} => q{Type array has unknown extra fields},
93 q{tts.base.examine._examine_type.unknown} => q{Unknown first argument of %s type to compile()},
94 q{tts.base.include._include.unknown} => q{Unknown include type: %s},
95 q{tts.base.include._interpolate.bogus_share} => q{Only SCALARs can be shared. You have tried to share a variable }
96 .q{type of %s named "%s". Consider converting it to a SCALAR or try }
97 .q{the monolith option to enable automatic variable sharing. }
98 .q{But please read the fine manual first},
99 q{tts.base.include._interpolate.bogus_share_notbare} => q{It looks like you've tried to share an expression (%s) instead of a simple variable.},
100 q{tts.base.parser._internal.id} => q{_internal(): id is missing},
101 q{tts.base.parser._internal.rv} => q{_internal(): id is invalid},
102 q{tts.base.parser._parse.unbalanced} => q{%d unbalanced %s delimiter(s) in template %s},
103 q{tts.cache.id.generate.data} => q{Can't generate id without data!},
104 q{tts.cache.id._custom.data} => q{Can't generate id without data!},
105 },
106 warning => {
107 q{tts.base.include.dynamic.recursion} => q{%s Deep recursion (>=%d) detected in the included file: %s},
108 }
109 };
110  
111 44 my @WHITESPACE_SYMBOLS = map { q{\\} . $_ } qw( r n f s );
112  
113 44 my $DEBUG = 0; # Disabled by default
114 44 my $DIGEST; # Will hold digester class name.
115  
116 3 ▶sub L {
Subroutine L: called CC=6 SCAR=19.6
Pod documented
117 3 my($type, $id, @param) = @_;
118 3 ▶ croak q{Type parameter to L() is missing} if ! $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 3 3
119 3 ▶ croak q{ID parameter ro L() is missing} if ! $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
120 3 ▶ my $root = $lang->{ $type } || croak "$type is not a valid L() type";
Condition50%1/2
$lang->{$type} || croak("$type is not a valid L() type")
l!l
30
MC/DC0%0/1
$lang->{$type} || croak("$type is not a valid L() type")
$lang->{$type}
Truth table50%1/2
A
A= $lang->{$type}
Aresult
11
00
121 3 ▶ my $value = $root->{ $id } || croak "$id is not a valid L() ID";
Condition50%1/2
$root->{$id} || croak("$id is not a valid L() ID")
l!l
30
MC/DC0%0/1
$root->{$id} || croak("$id is not a valid L() ID")
$root->{$id}
Truth table50%1/2
A
A= $root->{$id}
Aresult
11
00
122 3 ▶ return @param ? sprintf($value, @param) : $value;
Branch: 1 branch
BranchTrueFalseTotal
@param ? : 3 0 3
123 }
124  
125 2 ▶sub fatal {
Subroutine fatal: called CC=1 SCAR=0.0
Pod documented
126 2 my @args = @_;
127 2 return croak L( error => @args );
128 }
129  
130 720 ▶sub escape {
Subroutine escape: called CC=3 SCAR=11.2
Pod documented
131 720 my($c, $s) = @_;
132 720 ▶ fatal('tts.util.escape') if ! $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 0 720 720
133 720 ▶ return $s if ! $s; # false or undef
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 720 720
134 720 my $e = quotemeta $c;
135 720 $s =~ s{$e}{\\$c}xmsg;
136 720 return $s;
137 }
138  
139 166 ▶sub trim {
Subroutine trim: called CC=3 SCAR=11.2
Pod documented
140 166 my $s = shift;
141 166 ▶ return $s if ! $s; # false or undef
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 166 166
142 166 ▶ my $extra = shift || EMPTY_STRING;
Condition50%1/2
shift() || ''
l!l
0166
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
143 166 $s =~ s{\A \s+ }{$extra}xms;
144 166 $s =~ s{ \s+ \z}{$extra}xms;
145 166 return $s;
146 }
147  
148 23 ▶sub ltrim {
Subroutine ltrim: called CC=3 SCAR=11.0
Pod documented
149 23 my $s = shift;
150 23 ▶ return $s if ! $s; # false or undef
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 23 23
151 23 ▶ my $extra = shift || EMPTY_STRING;
Condition100%2/2
shift() || ''
l!l
518
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
152 23 $s =~ s{\A \s+ }{$extra}xms;
153 23 return $s;
154 }
155  
156 13 ▶sub rtrim {
Subroutine rtrim: called CC=3 SCAR=11.0
Pod documented
157 13 my $s = shift;
158 13 ▶ return $s if ! $s; # false or undef
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 13 13
159 13 ▶ my $extra = shift || EMPTY_STRING;
Condition100%2/2
shift() || ''
l!l
67
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
160 13 $s =~ s{ \s+ \z}{$extra}xms;
161 13 return $s;
162 }
163  
164 0 ▶sub visualize_whitespace {
Subroutine visualize_whitespace: not called CC=2 SCAR=17.9
Pod documented
165 0 my($str) = @_;
166 0 $str =~ s<[$_]><$_>xmsg for @WHITESPACE_SYMBOLS;
167 0 return $str;
168 }
169  
170 0 ▶*LOG = __PACKAGE__->can('MYLOG') || sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
171 0 my @args = @_;
172 0 ▶ my $self = ref $args[0] ? shift @args : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 0 0
173 0 my $id = shift @args;
174 0 my $message = shift @args;
175 0 ▶ $id = 'DEBUG' if not defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
176 0 ▶ $message = '<NO MESSAGE>' if not defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
177 0 $id =~ s{_}{ }xmsg;
178 0 $message = sprintf q{[ % 15s ] %s}, $id, $message;
179 0 warn "$message\n";
180 0 return;
181 44 ▶};
Condition33%1/3
__PACKAGE__->can('MYLOG') || sub { my(@args) = @_; my $self = ref $args[0] ? shift @args : undef; my $id = shift @args; my $message = shift @args; $id = 'DEBUG' unless defined $id; $message = '<NO MESSAGE>' unless defined $message; $id =~ s/_/ /gmsx; $message = sprintf('[ % 15s ] %s', $id, $message); warn "$message\n"; return; }
l!l&&r!l&&!r
0440
MC/DC0%0/2
__PACKAGE__->can('MYLOG') || sub { my(@args) = @_; my $self = ref $args[0] ? shift @args : undef; my $id = shift @args; my $message = shift @args; $id = 'DEBUG' unless defined $id; $message = '<NO MESSAGE>' unless defined $message; $id =~ s/_/ /gmsx; $message = sprintf('[ % 15s ] %s', $id, $message); warn "$message\n"; return; }
__PACKAGE__->can('MYLOG') sub { my(@args) = @_; my $self = ref $args[0] ? shift @args : undef; my $id = shift @args; my $message = shift @args; $id = 'DEBUG' unless defined $id; $message = '<NO MESSAGE>' unless defined $message; $id =~ s/_/ /gmsx; $message = sprintf('[ % 15s ] %s', $id, $message); warn "$message\n"; return; }
Truth table33%1/3
A || B
A= __PACKAGE__->can('MYLOG')B= sub { my(@args) = @_; my $self = ref $args[0] ? shift @args : undef; my $id = shift @args; my $message = shift @args; $id = 'DEBUG' unless defined $id; $message = '<NO MESSAGE>' unless defined $message; $id =~ s/_/ /gmsx; $message = sprintf('[ % 15s ] %s', $id, $message); warn "$message\n"; return; }
ABresult
1X1
011
000
182  
183 5103 ▶sub DEBUG {
Subroutine DEBUG: called CC=3 SCAR=11.0
Pod documented
184 5103 my $thing = shift;
185  
186 # so that one can use: $self->DEBUG or DEBUG
187 5103 ▶ $thing = shift if _is_parent_object( $thing );
Branch: 1 branch
BranchTrueFalseTotal
if _is_parent_object($thing) 1 5102 5103
188  
189 5103 ▶ $DEBUG = $thing+0 if defined $thing; # must be numeric
Branch: 1 branch
BranchTrueFalseTotal
if defined $thing 1 5102 5103
190 5103 return $DEBUG;
191 }
192  
193 13 ▶sub DIGEST {
Subroutine DIGEST: called CC=7 SCAR=22.2
Pod documented
194 13 ▶ return $DIGEST->new if $DIGEST;
Branch: 1 branch
BranchTrueFalseTotal
if $DIGEST 9 4 13
195  
196 4 local $SIG{__DIE__};
197 # local $@;
198 4 foreach my $mod ( DIGEST_MODS ) {
199 4 (my $file = $mod) =~ s{::}{/}xmsog;
200 4 $file .= '.pm';
201 4 my $ok = eval { require $file; };
202 4 ▶ if ( ! $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 4 4
203 0 ▶ LOG( FAILED => "$mod - $file" ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 0 0
204 0 next;
205 }
206 4 $DIGEST = $mod;
207 4 last;
208 }
209  
210 4 ▶ if ( not $DIGEST ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($DIGEST) 0 4 4
211 0 my @report = DIGEST_MODS;
212 0 my $last_error = pop @report;
213 0 fatal( 'tts.util.digest' => join(', ', @report), $last_error, $@ );
214 }
215  
216 4 ▶ LOG( DIGESTER => $DIGEST . ' v' . $DIGEST->VERSION ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 4 4
217 4 return $DIGEST->new;
218 }
219  
220 5103 ▶sub _is_parent_object {
Subroutine _is_parent_object: called CC=5 SCAR=20.9
221 5103 my $test = shift;
222 5103 ▶ return ! defined $test ? 0
Branch: 4 branches
BranchTrueFalseTotal
!defined($test) ? : 5102 1 5103
ref $test ? : 1 0 1
$test eq __PACKAGE__ ? : 0 0 0
$test eq 'Text::Template::Simple' ? : 0 0 0
223 : ref $test ? 1
224 : $test eq __PACKAGE__ ? 1
225 : $test eq PARENT ? 1
226 : 0
227 ;
228 }
229  
230 44 1;
231  
232 __END__
« Simple/Base/Examine.pm ↑ Summary Simple/Cache/ID.pm »