blib/lib/Text/Stencil.pm

filter: Statementstmt 96.6% 29 / 30 Branchbran 92.8% 13 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 4 / 5 Podpod 50.0% 1 / 2 totaltotal 92.1% 47 / 51 CC 8 SCAR 17.5
CC
8
Cov
95%
from_file
8.0
CLONE_SKIP
2.0
BEGIN
1.0
CRAP 5.8
↑ Summary eg/streaming.pl »
1 package Text::Stencil;
2 13 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 our $VERSION = '0.04';
5 13 require XSLoader;
6 13 XSLoader::load(__PACKAGE__, $VERSION);
7  
8 # The compiled template is a malloc'd C struct addressed by a raw pointer in the
9 # blessed IV; cloning it into a thread would give two interpreters one pointer
10 # to free.
11 0 ▶sub CLONE_SKIP { 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
12  
13 # Storable would copy that pointer into a second object and both would free it.
14 1 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
15 1 die "Text::Stencil: objects cannot be serialised; build one where you use it\n";
16 }
17  
18 16 ▶sub from_file {
Subroutine from_file: called CC=8 SCAR=20.8
Pod documented
19 16 my ($class, $file, %opts) = @_;
20 16 ▶ open my $fh, '<:raw', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "Text::Stencil: can't open ${file}: $!" 15 1 16
21 or die "Text::Stencil: can't open $file: $!";
22 15 local $/;
23 15 my $content = <$fh>;
24 # open can succeed on something that cannot be read -- a directory is the
25 # easy case -- and an unchecked read then built a silently empty renderer
26 # after warning from inside here. An empty file reads as a defined "", so
27 # this still accepts one.
28 15 ▶ defined $content
Branch: 1 branch
BranchTrueFalseTotal
defined $content or die "Text::Stencil: can't read ${file}: $!" 14 1 15
29 or die "Text::Stencil: can't read $file: $!";
30 14 close $fh;
31 14 require Encode;
32 14 $content = Encode::decode('UTF-8', $content, Encode::FB_CROAK());
33 # split on whichever section markers are present, in any order
34 12 my @parts = split /^__(HEADER|ROW|FOOTER)__[^\S\n]*(?:\n|\z)/m, $content;
35 12 ▶ if (@parts > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 1) 9 3 12
36 9 shift @parts; # text before the first marker
37 9 my %section;
38 9 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 17 9 26
39 17 my ($name, $body) = splice @parts, 0, 2;
40 17 ▶ $section{lc $name} = defined $body ? $body : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 13 4 17
41 }
42 die "Text::Stencil: $file has section markers but no __ROW__ section\n"
43 9 ▶ unless exists $section{row};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $section{"row"} 0 9 9
44 9 ▶ return $class->new(%opts, map { exists $section{$_} ? ($_ => $section{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $section{$_} ? : 17 10 27
45 qw(header row footer));
46 }
47 3 $class->new(%opts, row => $content);
48 }
49  
50 13 1;
51 __END__
↑ Summary eg/streaming.pl »