| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 36 | 9 | shift @parts; # text before the first marker | |||||||||
| 37 | 9 | my %section; | |||||||||
| 38 | 9 | ▶ | while (@parts) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 17 | my ($name, $body) = splice @parts, 0, 2; | |||||||||
| 40 | 17 | ▶ | $section{lc $name} = defined $body ? $body : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | } | ||||||||||
| 42 | die "Text::Stencil: $file has section markers but no __ROW__ section\n" | ||||||||||
| 43 | 9 | ▶ | unless exists $section{row}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 9 | ▶ | return $class->new(%opts, map { exists $section{$_} ? ($_ => $section{$_}) : () } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | qw(header row footer)); | ||||||||||
| 46 | } | ||||||||||
| 47 | 3 | $class->new(%opts, row => $content); | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 13 | 1; | |||||||||
| 51 | __END__ | ||||||||||