line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Sah::Format; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $DATE = '2017-07-10'; # DATE |
4
|
|
|
|
|
|
|
our $VERSION = '0.003'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
32
|
use 5.010001; |
|
2
|
|
|
|
|
6
|
|
7
|
2
|
|
|
2
|
|
12
|
use strict 'subs', 'vars'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
77
|
|
8
|
2
|
|
|
2
|
|
20
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
54
|
|
9
|
2
|
|
|
2
|
|
7544
|
use Log::ger; |
|
2
|
|
|
|
|
204
|
|
|
2
|
|
|
|
|
10
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
3192
|
use Data::Sah::FormatCommon; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
613
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our %SPEC; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $Log_Formatter_Code = $ENV{LOG_SAH_FORMATTER_CODE} // 0; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
$SPEC{gen_formatter} = { |
18
|
|
|
|
|
|
|
v => 1.1, |
19
|
|
|
|
|
|
|
summary => 'Generate formatter code', |
20
|
|
|
|
|
|
|
args => { |
21
|
|
|
|
|
|
|
%Data::Sah::FormatterCommon::gen_formatter_args, |
22
|
|
|
|
|
|
|
}, |
23
|
|
|
|
|
|
|
result_naked => 1, |
24
|
|
|
|
|
|
|
}; |
25
|
|
|
|
|
|
|
sub gen_formatter { |
26
|
10
|
|
|
10
|
1
|
61
|
my %args = @_; |
27
|
|
|
|
|
|
|
|
28
|
10
|
|
|
|
|
40
|
my $format = $args{format}; |
29
|
10
|
|
|
|
|
36
|
my $pkg = "Data::Sah::Format::perl\::$format"; |
30
|
10
|
|
|
|
|
92
|
(my $pkg_pm = "$pkg.pm") =~ s!::!/!g; |
31
|
|
|
|
|
|
|
|
32
|
10
|
|
|
|
|
1296
|
require $pkg_pm; |
33
|
|
|
|
|
|
|
|
34
|
10
|
|
|
|
|
93
|
my $fmt = &{"$pkg\::format"}( |
35
|
|
|
|
|
|
|
data_term => '$data', |
36
|
10
|
|
|
|
|
57
|
(args => $args{formatter_args}) x !!defined($args{formatter_args}), |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
10
|
|
|
|
|
29
|
my $code; |
40
|
|
|
|
|
|
|
|
41
|
10
|
|
|
|
|
25
|
my $code_require .= ''; |
42
|
|
|
|
|
|
|
#my %mem; |
43
|
10
|
50
|
|
|
|
38
|
if ($fmt->{modules}) { |
44
|
0
|
|
|
|
|
0
|
for my $mod (keys %{$fmt->{modules}}) { |
|
0
|
|
|
|
|
0
|
|
45
|
|
|
|
|
|
|
#next if $mem{$mod}++; |
46
|
0
|
|
|
|
|
0
|
$code_require .= "require $mod;\n"; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
10
|
|
|
|
|
64
|
$code = join( |
51
|
|
|
|
|
|
|
"", |
52
|
|
|
|
|
|
|
$code_require, |
53
|
|
|
|
|
|
|
"sub {\n", |
54
|
|
|
|
|
|
|
" my \$data = shift;\n", |
55
|
|
|
|
|
|
|
" $fmt->{expr};\n", |
56
|
|
|
|
|
|
|
"}", |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
10
|
50
|
|
|
|
36
|
if ($Log_Formatter_Code) { |
60
|
0
|
|
|
|
|
0
|
log_trace("Formatter code (gen args: %s): %s", \%args, $code); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
10
|
50
|
|
|
|
35
|
return $code if $args{source}; |
64
|
|
|
|
|
|
|
|
65
|
10
|
|
|
|
|
2201
|
my $formatter = eval $code; |
66
|
10
|
50
|
|
|
|
55
|
die if $@; |
67
|
10
|
|
|
|
|
90
|
$formatter; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |
71
|
|
|
|
|
|
|
# ABSTRACT: Formatter for Data::Sah |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
__END__ |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=pod |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=encoding UTF-8 |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 NAME |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Data::Sah::Format - Formatter for Data::Sah |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 VERSION |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
This document describes version 0.003 of Data::Sah::Format (from Perl distribution Data-Sah-Format), released on 2017-07-10. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 SYNOPSIS |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
use Data::Sah::Format qw(gen_formatter); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $c = gen_formatter( |
92
|
|
|
|
|
|
|
format => 'iso8601_date', |
93
|
|
|
|
|
|
|
#format_args => {...}, |
94
|
|
|
|
|
|
|
); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
my $val; |
97
|
|
|
|
|
|
|
$val = $c->(1465784006); # "2016-06-13" |
98
|
|
|
|
|
|
|
$val = $c->(DateTime->new(year=>2016, month=>6, day=>13)); # "2016-06-13" |
99
|
|
|
|
|
|
|
$val = $c->("2016-06-13"); # unchanged |
100
|
|
|
|
|
|
|
$val = $c->("9999-99-99"); # unchanged |
101
|
|
|
|
|
|
|
$val = $c->("foo"); # unchanged |
102
|
|
|
|
|
|
|
$val = $c->([]); # unchanged |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 DESCRIPTION |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 VARIABLES |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head2 $Log_Formatter_Code => bool (default: from ENV or 0) |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
If set to true, will log the generated formatter code (currently using |
111
|
|
|
|
|
|
|
L<Log::ger> at trace level). To see the log message, e.g. to the screen, you can |
112
|
|
|
|
|
|
|
use something like: |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
% TRACE=1 perl -MLog::ger::LevelFromEnv -MLog::ger::Output=Screen \ |
115
|
|
|
|
|
|
|
-MData::Sah::Format=gen_formatter -E'my $c = gen_formatter(...)' |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 FUNCTIONS |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head2 gen_formatter |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Usage: |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
gen_formatter() -> any |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Generate formatter code. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
This function is not exported. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
No arguments. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Return value: (any) |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 ENVIRONMENT |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 LOG_SAH_FORMATTER_CODE => bool |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Set default for C<$Log_Formatter_Code>. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 HOMEPAGE |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-Format>. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 SOURCE |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Source repository is at L<https://github.com/perlancar/perl-Data-Sah-Format>. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 BUGS |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-Format> |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
153
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
154
|
|
|
|
|
|
|
feature. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 SEE ALSO |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
L<Data::Sah> |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
L<Data::Sah::FormatterJS> |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
L<App::SahUtils>, including L<format-with-sah> to conveniently test formatting |
163
|
|
|
|
|
|
|
from the command-line. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 AUTHOR |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
perlancar <perlancar@cpan.org> |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
This software is copyright (c) 2017, 2016 by perlancar@cpan.org. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
174
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |