Templ/Util.pm

filter: Statementstmt 84.8% 56 / 66 Branchbran 87.5% 7 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 82.3% 14 / 17 Podpod 0.0% 0 / 9 totaltotal 74.2% 78 / 105 CC 7 SCAR 5.4
CC
7
Cov
83%
default
3.1
expand_isa
3.0
undent
2.0
CRAP 1.7
« Templ/Tag/Print.pm ↑ Summary Templ/Tag/Filter.pm »
1 package Templ::Util;
2  
3 1 require Exporter;
4 1 our @ISA = qw(Exporter);
5 our @EXPORT_OK
6 1 = qw(attempt quote unquote default indent undent number_lines headerize expand_isa);
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Run a block of code, return 0 if the code died, 1 if it didn't
14 2 ▶sub attempt (&) {
Subroutine attempt: called CC=2 SCAR=6.9
Pod undocumented
15 2 my $sub = shift;
16 2 eval { $sub->(); };
17 2 ▶ return $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 1 1 2
18 }
19  
20 # Escapes a single-quoted string
21 4 ▶sub quote ($) {
Subroutine quote: called CC=1 SCAR=0.0
Pod undocumented
22 4 my $str = shift;
23 4 $str =~ s|\\|\\\\|gs;
24 4 $str =~ s|'|\\'|gs;
25 4 return $str;
26 }
27  
28 # Escapes a single-quoted string
29 18 ▶sub unquote ($) {
Subroutine unquote: called CC=1 SCAR=0.0
Pod undocumented
30 18 my $str = shift;
31 18 $str =~ s|\\\\|\\|gs;
32 18 $str =~ s|\\'|'|gs;
33 18 return $str;
34 }
35  
36 # Returns the first defined value in a list, or a blank string if there are
37 # no defined values
38 51 ▶sub default (@) {
Subroutine default: called CC=3 SCAR=11.2
Pod undocumented
39 51 ▶ foreach (@_) { defined($_) && return $_; }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and return $_ 51 72 123
40 0 return '';
41 }
42  
43 1 ▶sub undent ($) {
Subroutine undent: called CC=2 SCAR=6.9
Pod undocumented
44 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶ return $_[0] if ( $_[0] !~ m/^(?:\r?\n)*([ \t]+)/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^(?:\r?\n)*([ \t]+)/ 0 1 1
46 1 my $i = $1;
47 1 return join '', map { s/^\Q$i\E//; $_ } grep { $_ ne '' }
48 split /(.*?\n)/, $_[0];
49 }
50  
51 0 ▶sub indent ($$) {
Subroutine indent: not called CC=1 SCAR=1.2
Pod undocumented
52 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 0 return join '', map {"$_[0]$_"} grep { $_ ne '' } split /(.*?\n)/, $_[1];
54 }
55  
56 0 ▶sub headerize ($$) {
Subroutine headerize: not called CC=1 SCAR=0.6
Pod undocumented
57 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 0 return join '', map {"$_[0]$_\n"} split /\n/, $_[1];
59 }
60  
61 0 ▶sub number_lines ($) {
Subroutine number_lines: not called CC=1 SCAR=1.7
Pod undocumented
62 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 0 my @lines = split /\n/, $_[0];
64 0 my $format = '%' . length( scalar(@lines) . '' ) . "s: %s\n";
65 0 return join '', map { sprintf( $format, ( $_ + 1 ), $lines[$_] ) }
66 0 .. $#lines;
67 }
68  
69 # Inspired by Class::ISA::self_and_super_path
70 38 ▶sub expand_isa {
Subroutine expand_isa: called CC=3 SCAR=11.0
Pod undocumented
71 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 38 ▶ my $seen = ref($_[0]) eq 'HASH' ? shift @_ : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 30 8 38
73 return
74 72 map { s/^::/main::/; $_ }
75 72 ▶ grep { defined($_) && $_ ne '' }
Condition33%1/3
defined $_ && $_ ne ''
!ll&&!rl&&r
0072
MC/DC0%0/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table33%1/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
76 30 map { $_, expand_isa( $seen, @{ $_ . '::ISA' } ) }
77 38 grep { !$seen->{$_}++ }
78 @_;
79 }
80  
81 1 1;
« Templ/Tag/Print.pm ↑ Summary Templ/Tag/Filter.pm »