line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
13704
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
44
|
|
2
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
65
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Template::Resolver; |
5
|
|
|
|
|
|
|
$Template::Resolver::VERSION = '1.13'; |
6
|
|
|
|
|
|
|
# ABSTRACT: A powerful, and simple, library for resolving placeholders in templated files |
7
|
|
|
|
|
|
|
# PODNAME: Template::Resolver |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
7
|
use Carp; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
81
|
|
10
|
2
|
|
|
2
|
|
5
|
use Log::Any; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
8
|
|
11
|
2
|
|
|
2
|
|
52
|
use Scalar::Util qw(blessed); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
106
|
|
12
|
2
|
|
|
2
|
|
580
|
use Template::Transformer; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1679
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $logger = Log::Any->get_logger(); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new { |
17
|
10
|
|
|
10
|
1
|
12359
|
return bless( {}, shift )->_init(@_); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub _entity_to_properties { |
21
|
59
|
|
|
59
|
|
75
|
my ( $entity, $properties, $prefix ) = @_; |
22
|
|
|
|
|
|
|
|
23
|
59
|
100
|
|
|
|
85
|
$properties = {} unless $properties; |
24
|
|
|
|
|
|
|
|
25
|
59
|
|
|
|
|
48
|
my $ref = ref($entity); |
26
|
59
|
100
|
66
|
|
|
282
|
if ( ( $ref && $ref eq 'HASH' ) || blessed($entity) ) { |
|
|
50
|
66
|
|
|
|
|
|
|
50
|
33
|
|
|
|
|
27
|
40
|
|
|
|
|
25
|
foreach my $key ( keys( %{$entity} ) ) { |
|
40
|
|
|
|
|
74
|
|
28
|
49
|
100
|
|
|
|
117
|
_entity_to_properties( $entity->{$key}, $properties, |
29
|
|
|
|
|
|
|
( $prefix ? "$prefix.$key" : $key ) ); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
elsif ( $ref && $ref eq 'ARRAY' ) { |
33
|
0
|
|
|
|
|
0
|
my $index = 0; |
34
|
0
|
|
|
|
|
0
|
foreach my $array_entity ( @{$entity} ) { |
|
0
|
|
|
|
|
0
|
|
35
|
0
|
0
|
|
|
|
0
|
_entity_to_properties( $array_entity, $properties, |
36
|
|
|
|
|
|
|
( $prefix ? "$prefix\[$index\]" : "[$index]" ) ); |
37
|
0
|
|
|
|
|
0
|
$index++; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
elsif ($ref) { |
41
|
0
|
|
|
|
|
0
|
croak("unsupported ref type '$ref'"); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
else { |
44
|
19
|
|
|
|
|
54
|
$properties->{$prefix} = $entity; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
59
|
|
|
|
|
150
|
return $properties; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _get_property { |
51
|
10
|
|
|
10
|
|
26
|
my ( $self, $value, $transform ) = @_; |
52
|
10
|
|
|
|
|
38
|
my $transformed = $self->{transformer}->transform( $value, $transform ); |
53
|
10
|
0
|
|
|
|
26
|
croak( "undefined value $value" . ( $transform ? ", using transform $transform" : '' ) ) |
|
|
50
|
|
|
|
|
|
54
|
|
|
|
|
|
|
unless ( defined($transformed) ); |
55
|
10
|
|
|
|
|
32
|
return $transformed; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _init { |
59
|
10
|
|
|
10
|
|
19
|
my ( $self, $entity, %options ) = @_; |
60
|
|
|
|
|
|
|
|
61
|
10
|
|
33
|
|
|
45
|
my $os = $options{os} || $^O; |
62
|
|
|
|
|
|
|
|
63
|
10
|
|
|
|
|
37
|
$logger->debug('creating new Resolver'); |
64
|
|
|
|
|
|
|
|
65
|
10
|
|
|
|
|
1548
|
$self->{entity} = $entity; |
66
|
|
|
|
|
|
|
$self->{transformer} = Template::Transformer->new( |
67
|
|
|
|
|
|
|
$os, |
68
|
|
|
|
|
|
|
_entity_to_properties($entity), |
69
|
|
|
|
|
|
|
( $options{additional_transforms} |
70
|
|
|
|
|
|
|
? ( additional_transforms => $options{additional_transforms} ) |
71
|
10
|
100
|
|
|
|
32
|
: () |
72
|
|
|
|
|
|
|
) |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
|
75
|
10
|
|
|
|
|
55
|
return $self; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub resolve { |
79
|
11
|
|
|
11
|
1
|
36
|
my ( $self, %options ) = @_; |
80
|
|
|
|
|
|
|
|
81
|
11
|
|
100
|
|
|
27
|
my $key = $options{key} || 'TEMPLATE'; |
82
|
|
|
|
|
|
|
|
83
|
11
|
|
|
|
|
10
|
my $content; |
84
|
11
|
100
|
|
|
|
31
|
if ( $options{content} ) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
85
|
2
|
|
|
|
|
3
|
$content = $options{content}; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
elsif ( $options{handle} ) { |
88
|
0
|
|
|
|
|
0
|
$content = do { local ($/) = undef; <$options{handle}> }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
elsif ( $options{filename} ) { |
91
|
9
|
|
|
|
|
8
|
$content = do { local ( @ARGV, $/ ) = $options{filename}; <> }; |
|
9
|
|
|
|
|
42
|
|
|
9
|
|
|
|
|
638
|
|
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
0
|
|
|
|
|
0
|
croak('Must provide one of [content, handle, filename]'); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
11
|
|
|
|
|
147
|
$content =~ s/\$\{$key(?:_(.*?))?\{(.*?)\}\}/$self->_get_property($2,$1)/egs; |
|
10
|
|
|
|
|
28
|
|
98
|
11
|
|
|
|
|
94
|
return $content; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
__END__ |