| 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
MC/DC0%0/2
$engine ||= "Template"->new
$engine
"Template"->new
Truth table66%2/3
A ||= B
A= $engineB= "Template"->new
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||
| 34 | 10 | ▶ | my %vars = %{peek_my(1)||{}}; | ||||||||||||||||||||||
|
Condition50%1/2
peek_my(1) || {}
MC/DC0%0/1
peek_my(1) || {}
peek_my(1)
Truth table50%1/2
A
A= peek_my(1)
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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
/^(\040+)/ and length $1
MC/DC50%1/2
/^(\040+)/ && length $1 || 0
/^(\040+)/
length $1
Truth table66%2/3
A and B
A= /^(\040+)/B= length $1
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 81 | 11 | return $stripped; | |||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||
| 84 | 3 | 1; | |||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||