| 1 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||
| 2 | package Text::Template::Simple::Base::Include; | ||||||||||||||||||||
| 3 | 32 | $Text::Template::Simple::Base::Include::VERSION = '0.91'; | |||||||||||||||||||
| 4 | 32 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 32 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 32 | ▶ | use Text::Template::Simple::Util qw(:all); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 32 | ▶ | use Text::Template::Simple::Constants qw(:all); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 32 | ▶ | use constant E_IN_MONOLITH => | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 'qq~%s Interpolated includes don\'t work under monolith option. ' | ||||||||||||||||||||
| 11 | .'Please disable monolith and use the \'SHARE\' directive in the include ' | ||||||||||||||||||||
| 12 | 32 | .'command: %s~'; | |||||||||||||||||||
| 13 | 32 | ▶ | use constant E_IN_DIR => q(q~%s '%s' is a directory~); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 32 | ▶ | use constant E_IN_SLURP => 'q~%s %s~'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 32 | ▶ | use constant TYPE_MAP => qw( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | @ ARRAY | ||||||||||||||||||||
| 17 | % HASH | ||||||||||||||||||||
| 18 | * GLOB | ||||||||||||||||||||
| 19 | \ REFERENCE | ||||||||||||||||||||
| 20 | 32 | ); | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 124 | ▶ | sub _include_no_monolith { | ||||||||||||||||||
|
Subroutine
_include_no_monolith: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | # no monolith eh? | ||||||||||||||||||||
| 24 | 124 | my($self, $type, $file, $opt) = @_; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 124 | my $rv = $self->_mini_compiler( | |||||||||||||||||||
| 27 | $self->_internal('no_monolith') => { | ||||||||||||||||||||
| 28 | OBJECT => $self->[FAKER_SELF], | ||||||||||||||||||||
| 29 | FILE => escape(q{~} => $file), | ||||||||||||||||||||
| 30 | TYPE => escape(q{~} => $type), | ||||||||||||||||||||
| 31 | } => { | ||||||||||||||||||||
| 32 | flatten => 1, | ||||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||
| 35 | 124 | ++$self->[NEEDS_OBJECT]; | |||||||||||||||||||
| 36 | 124 | return $rv; | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 9 | ▶ | sub _include_static { | ||||||||||||||||||
|
Subroutine
_include_static: called CC=2 SCAR=7.2
| |||||||||||||||||||||
| 40 | 9 | my($self, $file, $text, $err, $opt) = @_; | |||||||||||||||||||
| 41 | 9 | ▶ | return $self->[MONOLITH] | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | ? sprintf('q~%s~;', escape(q{~} => $text)) | ||||||||||||||||||||
| 43 | : $self->_include_no_monolith( T_STATIC, $file, $opt ) | ||||||||||||||||||||
| 44 | ; | ||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 118 | ▶ | sub _include_dynamic { | ||||||||||||||||||
|
Subroutine
_include_dynamic: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 48 | 118 | my($self, $file, $text, $err, $opt) = @_; | |||||||||||||||||||
| 49 | 118 | my $rv = EMPTY_STRING; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 118 | ++$self->[INSIDE_INCLUDE]; | |||||||||||||||||||
| 52 | 118 | ▶ | $self->[COUNTER_INCLUDE] ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$self->[21] ||= {}
MC/DC100%1/1
$self->[21] ||= {}
$self->[21]
Truth table100%2/2
A
A= $self->[21]
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # ++$self->[COUNTER_INCLUDE]{ $file } if $self->[TYPE_FILE] eq $file; | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 118 | ▶ | if ( ++$self->[COUNTER_INCLUDE]{ $file } >= MAX_RECURSION ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | # failsafe | ||||||||||||||||||||
| 58 | 1 | $self->[DEEP_RECURSION] = 1; | |||||||||||||||||||
| 59 | 1 | ▶ | LOG( DEEP_RECURSION => $file ) if DEBUG; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | 1 | my $w = L( warning => 'tts.base.include.dynamic.recursion', | |||||||||||||||||||
| 61 | $err, MAX_RECURSION, $file ); | ||||||||||||||||||||
| 62 | 1 | $rv .= sprintf 'q~%s~', escape( q{~} => $w ); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||
| 65 | # local stuff is for file name access through $0 in templates | ||||||||||||||||||||
| 66 | 117 | ▶ | $rv .= $self->[MONOLITH] | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | ? $self->_include_dynamic_monolith( $file, $text ) | ||||||||||||||||||||
| 68 | : $self->_include_no_monolith( T_DYNAMIC, $file, $opt ) | ||||||||||||||||||||
| 69 | ; | ||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 118 | --$self->[INSIDE_INCLUDE]; # critical: always adjust this | |||||||||||||||||||
| 73 | 118 | return $rv; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 2 | ▶ | sub _include_dynamic_monolith { | ||||||||||||||||||
|
Subroutine
_include_dynamic_monolith: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 77 | 2 | my($self,$file, $text) = @_; | |||||||||||||||||||
| 78 | 2 | my $old = $self->[FILENAME]; | |||||||||||||||||||
| 79 | 2 | $self->[FILENAME] = $file; | |||||||||||||||||||
| 80 | 2 | my $result = $self->_parse( $text ); | |||||||||||||||||||
| 81 | 2 | $self->[FILENAME] = $old; | |||||||||||||||||||
| 82 | 2 | return $result; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 137 | ▶ | sub include { | ||||||||||||||||||
|
Subroutine
include: called CC=14 SCAR=26.9
Pod
documented
| |||||||||||||||||||||
| 86 | 137 | my $self = shift; | |||||||||||||||||||
| 87 | 137 | ▶ | my $type = shift || 0; | ||||||||||||||||||
|
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 88 | 137 | my $file = shift; | |||||||||||||||||||
| 89 | 137 | my $opt = shift; | |||||||||||||||||||
| 90 | 137 | ▶ | my $is_static = T_STATIC == $type ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 137 | ▶ | my $is_dynamic = T_DYNAMIC == $type ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 137 | ▶ | my $known = $is_static || $is_dynamic; | ||||||||||||||||||
|
Condition66%2/3
$is_static || $is_dynamic
MC/DC0%0/2
$is_static || $is_dynamic
$is_static
$is_dynamic
Truth table66%2/3
A || B
A= $is_staticB= $is_dynamic
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 137 | ▶ | fatal('tts.base.include._include.unknown', $type) if not $known; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 137 | $file = trim $file; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 137 | my $err = $self->_include_error( $type ); | |||||||||||||||||||
| 99 | 137 | my $exists = $self->io->file_exists( $file ); | |||||||||||||||||||
| 100 | 137 | my $interpolate; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 137 | ▶ | if ( $exists ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 127 | $file = $exists; # file path correction | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||
| 106 | 10 | $interpolate = 1; # just guessing ... | |||||||||||||||||||
| 107 | 10 | ▶ | return sprintf E_IN_MONOLITH, $err, $file if $self->[MONOLITH]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 136 | ▶ | if ( $self->io->is_dir( $file ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 0 | return sprintf E_IN_DIR, $err, escape(q{~} => $file); | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 136 | ▶ | $self->_debug_include_type( $file, $type ) if DEBUG; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 136 | ▶ | if ( $interpolate ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 9 | my $rv = $self->_interpolate( $file, $type ); | |||||||||||||||||||
| 118 | 9 | $self->[NEEDS_OBJECT]++; | |||||||||||||||||||
| 119 | 9 | ▶ | LOG(INTERPOLATE_INC => "TYPE: $type; DATA: $file; RV: $rv") if DEBUG; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 9 | return $rv; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 127 | my $text = eval { $self->io->slurp($file); }; | |||||||||||||||||||
| 124 | 127 | ▶ | if ( $@ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 0 | return sprintf E_IN_SLURP, $err, $@; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 127 | ▶ | my $meth = '_include_' . ($is_dynamic ? 'dynamic' : 'static'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 127 | return $self->$meth( $file, $text, $err, $opt ); | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 0 | ▶ | sub _debug_include_type { | ||||||||||||||||||
|
Subroutine
_debug_include_type: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 133 | 0 | my($self, $file, $type) = @_; | |||||||||||||||||||
| 134 | 0 | require Text::Template::Simple::Tokenizer; | |||||||||||||||||||
| 135 | my $toke = Text::Template::Simple::Tokenizer->new( | ||||||||||||||||||||
| 136 | 0 | @{ $self->[DELIMITERS] }, | |||||||||||||||||||
| 137 | $self->[PRE_CHOMP], | ||||||||||||||||||||
| 138 | $self->[POST_CHOMP] | ||||||||||||||||||||
| 139 | ); | ||||||||||||||||||||
| 140 | 0 | LOG( INCLUDE => $toke->_visualize_tid($type) . " => '$file'" ); | |||||||||||||||||||
| 141 | 0 | return; | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 9 | ▶ | sub _interpolate { | ||||||||||||||||||
|
Subroutine
_interpolate: called CC=6 SCAR=17.9
| |||||||||||||||||||||
| 145 | 9 | my $self = shift; | |||||||||||||||||||
| 146 | 9 | my $file = shift; | |||||||||||||||||||
| 147 | 9 | my $type = shift; | |||||||||||||||||||
| 148 | 9 | my $etitle = $self->_include_error($type); | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | # so that, you can pass parameters, apply filters etc. | ||||||||||||||||||||
| 151 | 9 | my %inc = (INCLUDE => map { trim $_ } split RE_PIPE_SPLIT, $file ); | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 9 | ▶ | if ( $self->io->file_exists( $inc{INCLUDE} ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | # well... constantly working around :p | ||||||||||||||||||||
| 155 | 5 | $inc{INCLUDE} = qq{'$inc{INCLUDE}'}; | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | # die "You can not pass parameters to static includes" | ||||||||||||||||||||
| 159 | # if $inc{PARAM} && T_STATIC == $type; | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 9 | ▶ | $self->_interpolate_share_setup( \%inc ) if $inc{SHARE}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 9 | ▶ | my $share = $inc{SHARE} ? sprintf(q{'%s', %s}, ($inc{SHARE}) x 2) : 'undef'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | 9 | ▶ | my $filter = $inc{FILTER} ? escape( q{'} => $inc{FILTER} ) : EMPTY_STRING; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | return | ||||||||||||||||||||
| 168 | $self->_mini_compiler( | ||||||||||||||||||||
| 169 | $self->_internal('sub_include') => { | ||||||||||||||||||||
| 170 | OBJECT => $self->[FAKER_SELF], | ||||||||||||||||||||
| 171 | INCLUDE => escape( q{'} => $inc{INCLUDE} ), | ||||||||||||||||||||
| 172 | ERROR_TITLE => escape( q{'} => $etitle ), | ||||||||||||||||||||
| 173 | TYPE => $type, | ||||||||||||||||||||
| 174 | 9 | ▶ | PARAMS => $inc{PARAM} ? qq{[$inc{PARAM}]} : 'undef', | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 175 | FILTER => $filter, | ||||||||||||||||||||
| 176 | SHARE => $share, | ||||||||||||||||||||
| 177 | } => { | ||||||||||||||||||||
| 178 | flatten => 1, | ||||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | ); | ||||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 3 | ▶ | sub _interpolate_share_setup { | ||||||||||||||||||
|
Subroutine
_interpolate_share_setup: called CC=5 SCAR=18.2
| |||||||||||||||||||||
| 184 | 3 | my($self, $inc) = @_; | |||||||||||||||||||
| 185 | 3 | my @vars = map { trim $_ } split RE_FILTER_SPLIT, $inc->{SHARE}; | |||||||||||||||||||
| 186 | 3 | my %type = TYPE_MAP; | |||||||||||||||||||
| 187 | 3 | my @buf; | |||||||||||||||||||
| 188 | 3 | foreach my $var ( @vars ) { | |||||||||||||||||||
| 189 | 5 | ▶ | if ( $var !~ m{ \A \$ }xms ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 190 | 0 | my($char) = $var =~ m{ \A (.) }xms; | |||||||||||||||||||
| 191 | 0 | ▶ | my $type_name = $type{ $char } || '<UNKNOWN>'; | ||||||||||||||||||
|
Condition0%0/2
$type{$char} || '<UNKNOWN>'
MC/DC0%0/1
$type{$char} || '<UNKNOWN>'
$type{$char}
Truth table0%0/2
A
A= $type{$char}
| |||||||||||||||||||||
| 192 | 0 | fatal('tts.base.include._interpolate.bogus_share', $type_name, $var); | |||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | 5 | $var =~ tr/;//d; | |||||||||||||||||||
| 195 | 5 | ▶ | if ( $var =~ m{ [^a-zA-Z0-9_\$] }xms ) { ## no critic (ProhibitEnumeratedClasses) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 196 | 0 | fatal('tts.base.include._interpolate.bogus_share_notbare', $var); | |||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | 5 | push @buf, $var; | |||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | 3 | $inc->{SHARE} = join q{,}, @buf; | |||||||||||||||||||
| 201 | 3 | return; | |||||||||||||||||||
| 202 | } | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | 279 | ▶ | sub _include_error { | ||||||||||||||||||
|
Subroutine
_include_error: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 205 | 279 | my($self, $type) = @_; | |||||||||||||||||||
| 206 | 279 | ▶ | my $val = T_DYNAMIC == $type ? 'dynamic' | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 207 | : T_STATIC == $type ? 'static' | ||||||||||||||||||||
| 208 | : 'unknown' | ||||||||||||||||||||
| 209 | ; | ||||||||||||||||||||
| 210 | 279 | return sprintf '[ %s include error ]', $val; | |||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 32 | 1; | |||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | __END__ | ||||||||||||||||||||