blib/lib/Templ/Util.pm

filter: Statementstmt 84.8% 56 / 66 Branchbran 80.0% 8 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 82.3% 14 / 17 Podpod 0.0% 0 / 9 totaltotal 76.4% 78 / 102 SCAR 19.7
CC
7
Cov
84%
default
3.1
expand_isa
3.0
undent
2.0
CRAP 7.2
« blib/lib/Templ/Filter/HTML.pm ↑ Summary blib/lib/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 sub attempt (&) {
15 2 ▶ my $sub = shift;
Subroutine attempt: called CC=2 SCAR=6.9
Pod undocumented
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 sub quote ($) {
22 4 ▶ my $str = shift;
Subroutine quote: called CC=1 SCAR=0.0
Pod undocumented
23 4 $str =~ s|\\|\\\\|gs;
24 4 $str =~ s|'|\\'|gs;
25 4 return $str;
26 }
27  
28 # Escapes a single-quoted string
29 sub unquote ($) {
30 18 ▶ my $str = shift;
Subroutine unquote: called CC=1 SCAR=0.0
Pod undocumented
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 sub default (@) {
39 51 ▶ foreach (@_) { defined($_) && return $_; }
Subroutine default: called CC=3 SCAR=11.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and return $_ 51 72 123
40 0 return '';
41 }
42  
43 sub undent ($) {
44 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶ return $_[0] if ( $_[0] !~ m/^(?:\r?\n)*([ \t]+)/ );
Subroutine undent: called CC=2 SCAR=7.0
Pod undocumented
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 sub indent ($$) {
52 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 0 ▶ return join '', map {"$_[0]$_"} grep { $_ ne '' } split /(.*?\n)/, $_[1];
Subroutine indent: not called CC=1 SCAR=6.9
Pod undocumented
54 }
55  
56 sub headerize ($$) {
57 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 0 ▶ return join '', map {"$_[0]$_\n"} split /\n/, $_[1];
Subroutine headerize: not called CC=1 SCAR=6.9
Pod undocumented
59 }
60  
61 sub number_lines ($) {
62 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 0 ▶ my @lines = split /\n/, $_[0];
Subroutine number_lines: not called CC=1 SCAR=6.9
Pod undocumented
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 sub expand_isa {
71 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 38 ▶ my $seen = ref($_[0]) eq 'HASH' ? shift @_ : {};
Subroutine expand_isa: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 30 8 38
73 return
74 72 map { s/^::/main::/; $_ }
75 72 ▶ grep { defined($_) && $_ ne '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_ ne "" 72 0 72
76 30 map { $_, expand_isa( $seen, @{ $_ . '::ISA' } ) }
77 38 grep { !$seen->{$_}++ }
78 @_;
79 }
80  
81 1 1;
« blib/lib/Templ/Filter/HTML.pm ↑ Summary blib/lib/Templ/Tag/Filter.pm »