| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package String::Template; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
499710
|
use strict; |
|
|
2
|
|
|
|
|
14
|
|
|
|
2
|
|
|
|
|
61
|
|
|
4
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
46
|
|
|
5
|
2
|
|
|
2
|
|
55
|
use 5.008001; |
|
|
2
|
|
|
|
|
7
|
|
|
6
|
2
|
|
|
2
|
|
12
|
use base 'Exporter'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
202
|
|
|
7
|
2
|
|
|
2
|
|
17
|
use POSIX (); |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
35
|
|
|
8
|
2
|
|
|
2
|
|
1113
|
use Date::Parse; |
|
|
2
|
|
|
|
|
16130
|
|
|
|
2
|
|
|
|
|
2308
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# ABSTRACT: Fills in string templates from hash of fields |
|
11
|
|
|
|
|
|
|
our $VERSION = '0.23'; # VERSION |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our @EXPORT = qw(expand_string missing_values expand_stringi); |
|
15
|
|
|
|
|
|
|
our @EXPORT_OK = qw(expand_hash); |
|
16
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => [@EXPORT, @EXPORT_OK] ); |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my %special = |
|
19
|
|
|
|
|
|
|
( |
|
20
|
|
|
|
|
|
|
'%' => sub { sprintf("%$_[0]", $_[1]) }, |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
':' => sub { POSIX::strftime($_[0], localtime(str2time($_[1]))) }, |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
'!' => sub { POSIX::strftime($_[0], gmtime(str2time($_[1]))) }, |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
'#' => sub { my @args = split(/\s*,\s*/, $_[0]); |
|
27
|
|
|
|
|
|
|
defined $args[1] |
|
28
|
|
|
|
|
|
|
? substr($_[1], $args[0], $args[1]) |
|
29
|
|
|
|
|
|
|
: substr($_[1], $args[0]) } |
|
30
|
|
|
|
|
|
|
); |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $specials = join('', keys %special); |
|
33
|
|
|
|
|
|
|
my $specialre = qr/^([^{$specials]+)([{$specials])(.+)$/; |
|
34
|
|
|
|
|
|
|
my $bracketre = qr/^([^$specials]*?)([{$specials])(.*?)(?
|
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$special{'{'} = sub { |
|
37
|
|
|
|
|
|
|
my ($field, $replace) = @_; |
|
38
|
|
|
|
|
|
|
$field =~ s/\\\}/}/g; |
|
39
|
|
|
|
|
|
|
my ($pre, $key, $spec, $post) = $field =~ /$bracketre/; |
|
40
|
|
|
|
|
|
|
$pre . $special{$key}($spec, $replace) . $post; |
|
41
|
|
|
|
|
|
|
}; |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# |
|
44
|
|
|
|
|
|
|
# _replace($field, \%fields, $undef_flag) |
|
45
|
|
|
|
|
|
|
# |
|
46
|
|
|
|
|
|
|
# replace a single " or "" |
|
47
|
|
|
|
|
|
|
# or "" |
|
48
|
|
|
|
|
|
|
# |
|
49
|
|
|
|
|
|
|
sub _replace |
|
50
|
|
|
|
|
|
|
{ |
|
51
|
56
|
|
|
56
|
|
151
|
my ($field, $f, $undef_flag, $i_flag) = @_; |
|
52
|
|
|
|
|
|
|
|
|
53
|
56
|
100
|
|
|
|
332
|
if ($field =~ $specialre) |
|
54
|
|
|
|
|
|
|
{ |
|
55
|
23
|
100
|
|
|
|
122
|
return ($undef_flag ? "<$field>" : '') unless defined $f->{($i_flag ? lc($1) : $1)}; |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
56
|
18
|
100
|
|
|
|
99
|
return $special{$2}($3,$f->{($i_flag ? lc($1) : $1)}); |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
33
|
100
|
|
|
|
96
|
my $ifield = $i_flag ? lc $field : $field; |
|
60
|
33
|
100
|
|
|
|
156
|
return defined $f->{$ifield} ? $f->{$ifield} |
|
|
|
100
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
: ($undef_flag ? "<$field>" : ''); |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# |
|
66
|
|
|
|
|
|
|
# expand_string($string, \%fields, $undef_flag) |
|
67
|
|
|
|
|
|
|
# find "" |
|
68
|
|
|
|
|
|
|
# |
|
69
|
|
|
|
|
|
|
sub expand_string |
|
70
|
|
|
|
|
|
|
{ |
|
71
|
38
|
|
|
38
|
1
|
23268
|
my ($string, $fields, $undef_flag) = @_; |
|
72
|
|
|
|
|
|
|
|
|
73
|
38
|
|
|
|
|
207
|
$string =~ s/<([^>]+)>/_replace($1, $fields, $undef_flag)/ge; |
|
|
34
|
|
|
|
|
292
|
|
|
74
|
|
|
|
|
|
|
|
|
75
|
38
|
|
|
|
|
1793
|
return $string; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub expand_stringi |
|
80
|
|
|
|
|
|
|
{ |
|
81
|
9
|
|
|
9
|
1
|
3328
|
my ($string, $fields, $undef_flag) = @_; |
|
82
|
9
|
|
|
|
|
27
|
my %ifields = map { lc $_ => $fields->{$_} } keys %$fields; |
|
|
19
|
|
|
|
|
61
|
|
|
83
|
|
|
|
|
|
|
|
|
84
|
9
|
|
|
|
|
72
|
$string =~ s/<([^>]+)>/_replace($1, \%ifields, $undef_flag, 1)/gie; |
|
|
22
|
|
|
|
|
591
|
|
|
85
|
|
|
|
|
|
|
|
|
86
|
9
|
|
|
|
|
837
|
return $string; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub missing_values |
|
91
|
|
|
|
|
|
|
{ |
|
92
|
5
|
|
|
5
|
1
|
3959
|
my ($string, $fields, $dont_allow_undefs) = @_; |
|
93
|
5
|
|
|
|
|
7
|
my @missing; |
|
94
|
|
|
|
|
|
|
|
|
95
|
5
|
|
|
|
|
96
|
while ($string =~ /<([^>$specials]+)(?:[$specials][^>]+)?>/g) { |
|
96
|
12
|
100
|
100
|
|
|
72
|
next if exists($fields->{$1}) && (!$dont_allow_undefs || defined($fields->{$1})); |
|
|
|
|
100
|
|
|
|
|
|
97
|
6
|
|
|
|
|
35
|
push @missing, $1; |
|
98
|
|
|
|
|
|
|
} |
|
99
|
5
|
100
|
|
|
|
25
|
return unless @missing; |
|
100
|
3
|
|
|
|
|
15
|
return @missing; |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub expand_hash |
|
105
|
|
|
|
|
|
|
{ |
|
106
|
4
|
|
|
4
|
1
|
6465
|
my ($hash, $maxdepth) = @_; |
|
107
|
|
|
|
|
|
|
|
|
108
|
4
|
|
100
|
|
|
20
|
$maxdepth ||= 10; |
|
109
|
|
|
|
|
|
|
|
|
110
|
4
|
|
|
|
|
6
|
my $changeflag = 1; |
|
111
|
4
|
|
|
|
|
6
|
my $missing = 1; |
|
112
|
|
|
|
|
|
|
|
|
113
|
4
|
|
|
|
|
10
|
while ($changeflag) |
|
114
|
|
|
|
|
|
|
{ |
|
115
|
7
|
|
|
|
|
10
|
$changeflag = 0; |
|
116
|
7
|
|
|
|
|
10
|
$missing = 0; |
|
117
|
7
|
|
|
|
|
26
|
foreach my $key (sort keys %$hash) |
|
118
|
|
|
|
|
|
|
{ |
|
119
|
17
|
|
|
|
|
34
|
my $newstr = expand_string($hash->{$key}, $hash, 1); |
|
120
|
|
|
|
|
|
|
|
|
121
|
17
|
100
|
|
|
|
39
|
if ($newstr ne $hash->{$key}) |
|
122
|
|
|
|
|
|
|
{ |
|
123
|
6
|
|
|
|
|
11
|
$hash->{$key} = $newstr; |
|
124
|
6
|
|
|
|
|
8
|
$changeflag = 1; |
|
125
|
|
|
|
|
|
|
} |
|
126
|
17
|
100
|
|
|
|
41
|
$missing++ if $newstr =~ /<[^>]+>/; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
7
|
100
|
|
|
|
22
|
last unless --$maxdepth; |
|
129
|
|
|
|
|
|
|
} |
|
130
|
4
|
100
|
|
|
|
14
|
return $missing ? undef : 1; |
|
131
|
|
|
|
|
|
|
} |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
__END__ |