blib/lib/String/TT.pm

filter: Statementstmt 100% 64 / 64 Branchbran 91.6% 11 / 12 Conditioncond 70.0% 7 / 10 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 92.2% 95 / 103 CC 13 SCAR 15.2
CC
13
Cov
95%
tt
8.0
strip
5.0
_build_tt_engine
2.1
CRAP 4.6
↑ Summary
1 package String::TT;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use PadWalker qw(peek_my);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp qw(confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 exports => [qw/tt strip/],
10 3 };
11  
12 3 our $VERSION = '0.03';
13 3 our $AUTHORITY = 'CPAN:JROCKWAY';
14  
15 3 my %SIGIL_MAP = (
16 '$' => 's',
17 '@' => 'a',
18 '%' => 'h',
19 '&' => 'c', # probably do not need
20 '*' => 'g', # probably do not need
21 );
22  
23 {
24 3 my $engine;
25 10 ▶ sub _build_tt_engine {
Subroutine _build_tt_engine: called CC=2 SCAR=7.2
26 10 ▶ return $engine ||= Template->new;
Condition66%2/3
$engine ||= "Template"->new
l!l&&r!l&&!r
910
MC/DC0%0/2
$engine ||= "Template"->new
$engine "Template"->new
Truth table66%2/3
A ||= B
A= $engineB= "Template"->new
ABresult
1X1
011
000
27 }
28 }
29  
30 3 ▶sub tt($) {
Subroutine tt: called CC=8 SCAR=20.8
Pod documented
31 10 my $template = shift;
32 10 ▶ confess 'Whoa there, I need a template' if !defined $template;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template 0 10 10
33  
34 10 ▶ my %vars = %{peek_my(1)||{}};
Condition50%1/2
peek_my(1) || {}
l!l
100
MC/DC0%0/1
peek_my(1) || {}
peek_my(1)
Truth table50%1/2
A
A= peek_my(1)
Aresult
11
00
35 10 my %transformed_vars;
36 10 for my $v (keys %vars){
37 15 my ($sigil, $varname) = ($v =~ /^(.)(.+)$/);
38 15 my $suffix = $SIGIL_MAP{$sigil};
39 15 my $name = join '_', $varname, $suffix;
40 15 $transformed_vars{$name} = $vars{$v};
41 15 ▶ if($sigil eq '$'){
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq "\$") 12 3 15
42 12 $transformed_vars{$name} = ${$transformed_vars{$name}};
43 }
44 }
45  
46 # add the plain scalar variables (without overwriting anything)
47 10 for my $v (grep { /_s$/ } keys %transformed_vars) {
48 12 my ($varname) = ($v =~ /^(.+)_s$/);
49 12 ▶ if(!exists $transformed_vars{$varname}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $transformed_vars{$varname}) 11 1 12
50 11 $transformed_vars{$varname} = $transformed_vars{$v};
51 }
52 }
53  
54 10 my $t = _build_tt_engine;
55 10 my $output;
56 10 ▶ $t->process(\$template, \%transformed_vars, \$output)
Branch: 1 branch
BranchTrueFalseTotal
$t->process(\$template, \%transformed_vars, \$output) or croak($t->error) 9 1 10
57 || croak $t->error;
58 9 return $output;
59 }
60  
61 11 ▶sub strip($){
Subroutine strip: called CC=5 SCAR=16.1
Pod documented
62 11 my $lines = shift;
63  
64 11 my $trailing_newline = ($lines =~ /\n$/s);# perl silently throws away data
65 11 my @lines = split "\n", $lines;
66 11 ▶ shift @lines if $lines[0] eq ''; # strip empty leading line
Branch: 1 branch
BranchTrueFalseTotal
if $lines[0] eq "" 8 3 11
67  
68 # determine indentation level
69 11 ▶ my @spaces = map { /^(\040+)/ and length $1 or 0 } grep { !/^\s*$/ } @lines;
Condition80%4/5
/^(\040+)/ && length $1 || 0
l!l
143
/^(\040+)/ and length $1
!ll&&!rl&&r
3014
MC/DC50%1/2
/^(\040+)/ && length $1 || 0
/^(\040+)/ length $1
Truth table66%2/3
A and B
A= /^(\040+)/B= length $1
ABresult
111
0X0
100
70
71 11 my $indentation_level = min(@spaces);
72
73 # strip off $indentation_level spaces
74 my $stripped = join "\n", map {
75 11 my $copy = $_;
76 22 substr($copy,0,$indentation_level) = "";
77 22 $copy;
78 } @lines;
79
80 11 ▶ $stripped .= "\n" if $trailing_newline;
Branch: 1 branch
BranchTrueFalseTotal
if $trailing_newline 5 6 11
81 11 return $stripped;
82 }
83  
84 3 1;
85 __END__
↑ Summary