Data/Sah/Compiler/perl.pm

filter: Statementstmt 78.1% 175 / 224 Branchbran 62.8% 49 / 78 Conditioncond 65.6% 44 / 67 MC/DCmcdc 32.1% 9 / 28 Subroutinesub 83.6% 46 / 55 Podpod 8.5% 4 / 47 totaltotal 65.5% 327 / 499 CC 68 SCAR 23.6
CC
68
Cov
73%
add_module
41.7
compile
14.2
gen_cached_validator
12.0
CRAP 10.6
« Data/Sah/Human.pm ↑ Summary Data/Sah/Compiler/perl/TH/float.pm »
1 package Data::Sah::Compiler::perl;
2  
3 23 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 23 ▶use Data::Dmp qw(dmp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use Mo qw(build default);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 23 extends 'Data::Sah::Compiler::Prog';
12  
13 23 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
14 23 our $DATE = '2024-02-16'; # DATE
15 23 our $DIST = 'Data-Sah'; # DIST
16 23 our $VERSION = '0.917'; # VERSION
17  
18 23 our $PP;
19 23 our $CORE;
20 23 our $CORE_OR_PP;
21 23 our $NO_MODULES;
22  
23 # BEGIN COPIED FROM String::Indent
24 9949 ▶sub __indent {
Subroutine __indent: called CC=8 SCAR=23.4
25 9949 my ($indent, $str, $opts) = @_;
26 9949 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
09949
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
27  
28 9949 ▶ my $ibl = $opts->{indent_blank_lines} // 1;
Condition50%1/2
$opts->{"indent_blank_lines"} // 1
l!l
09949
MC/DC0%0/1
$opts->{"indent_blank_lines"} // 1
$opts->{"indent_blank_lines"}
Truth table50%1/2
A
A= $opts->{"indent_blank_lines"}
Aresult
11
00
29 9949 ▶ my $fli = $opts->{first_line_indent} // $indent;
Condition33%1/3
$opts->{"first_line_indent"} // $indent
l!l&&r!l&&!r
099490
MC/DC0%0/2
$opts->{"first_line_indent"} // $indent
$opts->{"first_line_indent"} $indent
Truth table33%1/3
A // B
A= $opts->{"first_line_indent"}B= $indent
ABresult
1X1
011
000
30 9949 ▶ my $sli = $opts->{subsequent_lines_indent} // $indent;
Condition33%1/3
$opts->{"subsequent_lines_indent"} // $indent
l!l&&r!l&&!r
099490
MC/DC0%0/2
$opts->{"subsequent_lines_indent"} // $indent
$opts->{"subsequent_lines_indent"} $indent
Truth table33%1/3
A // B
A= $opts->{"subsequent_lines_indent"}B= $indent
ABresult
1X1
011
000
31 #say "D:ibl=<$ibl>, fli=<$fli>, sli=<$sli>";
32  
33 9949 my $i = 0;
34 9949 ▶ $str =~ s/^([^\r\n]?)/$i++; !$ibl && !$1 ? "$1" : $i==1 ? "$fli$1" : "$sli$1"/egm;
Branch: 2 branches
BranchTrueFalseTotal
!($ibl || $1) ? : 0 221434 221434
$i == 1 ? : 9949 211485 221434
Condition33%1/3
$ibl || $1
l!l&&r!l&&!r
22143400
MC/DC0%0/2
$ibl || $1
$ibl $1
Truth table33%1/3
A || B
A= $iblB= $1
ABresult
1X1
011
000
35 9949 $str;
36 }
37 # END COPIED FROM String::Indent
38  
39 4751 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
40 4751 my ($self, $args) = @_;
41  
42 4751 $self->comment_style('shell');
43 4751 $self->indent_character(" " x 4);
44 4751 $self->var_sigil('$');
45 4751 $self->concat_op(".");
46 }
47  
48 25512 ▶sub name { "perl" }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
49  
50 40726 ▶sub literal {
Subroutine literal: called CC=1 SCAR=0.0
Pod undocumented
51 40726 dmp($_[1]);
52 }
53  
54 5060 ▶sub compile {
Subroutine compile: called CC=14 SCAR=26.6
Pod documented
55 5060 my ($self, %args) = @_;
56  
57 #$self->expr_compiler->compiler->hook_var(
58 # sub {
59 # $_[0];
60 # }
61 #);
62  
63 #$self->expr_compiler->compiler->hook_func(
64 # sub {
65 # my ($name, @args) = @_;
66 # die "Unknown function $name"
67 # unless $self->main->func_names->{$name};
68 # my $subname = "func_$name";
69 # $self->define_sub_start($subname);
70 # my $meth = "func_$name";
71 # $self->func_handlers->{$name}->$meth;
72 # $self->define_sub_end();
73 # $subname . "(" . join(", ", @args) . ")";
74 # }
75 #);
76  
77 # Data::Dumper is chosen as the default because it's core, but note here the
78 # inconveniences: 1) the incantation to use it the way we want is
79 # cumbersome. Storable is not feasible because of reason explained in
80 # comment in expr_dump(). Data::Dmp is another choice.
81 5060 ▶ $args{dump_module} //= "Data::Dumper";
Condition100%2/2
$args{"dump_module"} //= "Data::Dumper"
l!l
3304730
MC/DC100%1/1
$args{"dump_module"} //= "Data::Dumper"
$args{"dump_module"}
Truth table100%2/2
A
A= $args{"dump_module"}
Aresult
11
00
82  
83 5060 ▶ $args{pp} //= $PP // $ENV{DATA_SAH_PP} // 0;
Condition85%6/7
$args{"pp"} //= ($Data::Sah::Compiler::perl::PP // $ENV{"DATA_SAH_PP"}) // 0
l!l
3304730
($Data::Sah::Compiler::perl::PP // $ENV{"DATA_SAH_PP"}) // 0
l!l
404690
$Data::Sah::Compiler::perl::PP // $ENV{"DATA_SAH_PP"}
l!l&&r!l&&!r
4004690
MC/DC100%1/1
$args{"pp"} //= ($Data::Sah::Compiler::perl::PP // $ENV{"DATA_SAH_PP"}) // 0
$args{"pp"}
Truth table100%2/2
A
A= $args{"pp"}
Aresult
11
00
84 5060 ▶ $args{core} //= $CORE // $ENV{DATA_SAH_CORE} // 0;
Condition85%6/7
$args{"core"} //= ($Data::Sah::Compiler::perl::CORE // $ENV{"DATA_SAH_CORE"}) // 0
l!l
3304730
($Data::Sah::Compiler::perl::CORE // $ENV{"DATA_SAH_CORE"}) // 0
l!l
404690
$Data::Sah::Compiler::perl::CORE // $ENV{"DATA_SAH_CORE"}
l!l&&r!l&&!r
4004690
MC/DC100%1/1
$args{"core"} //= ($Data::Sah::Compiler::perl::CORE // $ENV{"DATA_SAH_CORE"}) // 0
$args{"core"}
Truth table100%2/2
A
A= $args{"core"}
Aresult
11
00
85 5060 ▶ $args{core_or_pp} //= $CORE_OR_PP // $ENV{DATA_SAH_CORE_OR_PP} // 0;
Condition85%6/7
$args{"core_or_pp"} //= ($Data::Sah::Compiler::perl::CORE_OR_PP // $ENV{"DATA_SAH_CORE_OR_PP"}) // 0
l!l
3304730
($Data::Sah::Compiler::perl::CORE_OR_PP // $ENV{"DATA_SAH_CORE_OR_PP"}) // 0
l!l
404690
$Data::Sah::Compiler::perl::CORE_OR_PP // $ENV{"DATA_SAH_CORE_OR_PP"}
l!l&&r!l&&!r
4004690
MC/DC100%1/1
$args{"core_or_pp"} //= ($Data::Sah::Compiler::perl::CORE_OR_PP // $ENV{"DATA_SAH_CORE_OR_PP"}) // 0
$args{"core_or_pp"}
Truth table100%2/2
A
A= $args{"core_or_pp"}
Aresult
11
00
86 5060 ▶ $args{no_modules} //= $NO_MODULES // $ENV{DATA_SAH_NO_MODULES} // 0;
Condition85%6/7
$args{"no_modules"} //= ($Data::Sah::Compiler::perl::NO_MODULES // $ENV{"DATA_SAH_NO_MODULES"}) // 0
l!l
3304730
($Data::Sah::Compiler::perl::NO_MODULES // $ENV{"DATA_SAH_NO_MODULES"}) // 0
l!l
404690
$Data::Sah::Compiler::perl::NO_MODULES // $ENV{"DATA_SAH_NO_MODULES"}
l!l&&r!l&&!r
4004690
MC/DC100%1/1
$args{"no_modules"} //= ($Data::Sah::Compiler::perl::NO_MODULES // $ENV{"DATA_SAH_NO_MODULES"}) // 0
$args{"no_modules"}
Truth table100%2/2
A
A= $args{"no_modules"}
Aresult
11
00
87  
88 5060 $self->SUPER::compile(%args);
89 }
90  
91 5060 ▶sub init_cd {
Subroutine init_cd: called CC=2 SCAR=6.9
Pod undocumented
92 5060 my ($self, %args) = @_;
93  
94 5060 my $cd = $self->SUPER::init_cd(%args);
95  
96 5060 ▶ $self->add_runtime_no($cd, 'warnings', ["'void'"]) unless $cd->{args}{no_modules};
Branch: 1 branch
BranchTrueFalseTotal
unless $cd->{"args"}{"no_modules"} 5020 40 5060
97  
98 5060 $cd;
99 }
100  
101 5060 ▶sub before_resolve {
Subroutine before_resolve: called CC=3 SCAR=11.7
Pod undocumented
102 5060 require Data::Cmp;
103  
104 5060 my ($self, $cd) = @_;
105  
106 # check whether we can optimize and produce a shorter, faster code under
107 # certain conditions (return_type=bool, simple schemas).
108  
109 5060 ▶ return 0 unless $cd->{args}{return_type} eq 'bool_valid';
Branch: 1 branch
BranchTrueFalseTotal
unless $cd->{"args"}{"return_type"} eq "bool_valid" 3152 1908 5060
110  
111 1908 my $nschema = $cd->{nschema};
112 1908 my $dt = $cd->{args}{data_term};
113 1908 ▶ if (Data::Cmp::cmp_data($nschema, ["int", {"req", 1}, {}]) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Cmp::cmp_data($nschema, ["int", {"req", 1}, {}]) == 0) 0 1908 1908
114 #$cd->{result} = "!defined($dt) || (!ref($dt) && length($dt) >= 4)";
115 0 $self->add_runtime_module($cd, 'Scalar::Util::Numeric');
116 0 $cd->{result} = "Scalar::Util::Numeric::isint($dt)";
117 0 return 99;
118 }
119  
120 1908 return;
121 }
122  
123 20764 ▶sub true { "1" }
Subroutine true: called CC=1 SCAR=0.0
Pod undocumented
124  
125 90 ▶sub false { "''" }
Subroutine false: called CC=1 SCAR=0.0
Pod undocumented
126  
127 # quick lookup table, to avoid having to use Module::CoreList or Module::XSOrPP
128 23 our %known_modules = (
129 'DateTime::Duration' => {pp=>1, core=>0},
130 'DateTime' => {pp=>0, core=>0},
131 'DateTime::Format::Alami' => {pp=>1, core=>0},
132 'DateTime::Format::Alami::EN' => {pp=>1, core=>0},
133 'DateTime::Format::Alami::ID' => {pp=>1, core=>0},
134 'DateTime::Format::Natural' => {pp=>1, core=>0},
135 'experimental' => {pp=>1, core=>0}, # only core in 5.020+, so we note it as 0
136 'List::Util' => {pp=>0, core=>1},
137 'Module::List::More' => {pp=>1, core=>0},
138 'PERLANCAR::Module::List' => {pp=>1, core=>0},
139 'Regexp::From::String' => {pp=>1, core=>0},
140 'Scalar::Util::Numeric' => {pp=>0, core=>0},
141 'Scalar::Util::Numeric::PP' => {pp=>1, core=>0},
142 'Scalar::Util' => {pp=>0, core=>1},
143 'Storable' => {pp=>0, core=>1},
144 'String::Wildcard::Bash' => {pp=>1, core=>0},
145 'Time::Duration::Parse::AsHash' => {pp=>1, core=>0},
146 'Time::Local' => {pp=>1, core=>1},
147 'Time::Moment' => {pp=>0, core=>0},
148 'Time::Piece' => {pp=>0, core=>1},
149 'warnings' => {pp=>1, core=>1},
150 );
151  
152 14100 ▶sub add_module {
Subroutine add_module: called CC=17 SCAR=37.3
Pod undocumented
153 14100 my ($self, $cd, $name, $extra_keys, $allow_duplicate) = @_;
154  
155 14100 ▶ if (exists $extra_keys->{core}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $extra_keys->{"core"}) 0 14100 14100
156 0 $known_modules{$name}{core} = $extra_keys->{core};
157 }
158  
159 14100 ▶ if (exists $extra_keys->{pp}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $extra_keys->{"pp"}) 0 14100 14100
160 0 $known_modules{$name}{pp} = $extra_keys->{pp};
161 }
162  
163 14100 ▶ if ($extra_keys->{phase} eq 'runtime') {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_keys->{"phase"} eq "runtime") 7653 6447 14100
164 7653 ▶ if ($cd->{args}{no_modules}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{"args"}{"no_modules"}) 0 7653 7653
165 0 die "BUG: Use of module '$name' when compile option no_modules=1";
166 }
167  
168 0 ▶ if ($cd->{args}{whitelist_modules} && grep { $_ eq $name } @{ $cd->{args}{whitelist_modules} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{"args"}{"whitelist_modules"} and grep {$_ eq $name;} @{$$cd{"args"}{"whitelist_modules"};}) 0 7653 7653
Condition33%1/3
$cd->{"args"}{"whitelist_modules"} and grep {$_ eq $name;} @{$$cd{"args"}{"whitelist_modules"};}
!ll&&!rl&&r
765300
MC/DC0%0/2
$cd->{"args"}{"whitelist_modules"} and grep {$_ eq $name;} @{$$cd{"args"}{"whitelist_modules"};}
$cd->{"args"}{"whitelist_modules"} grep {$_ eq $name;} @{$$cd{"args"}{"whitelist_modules"};}
Truth table33%1/3
A and B
A= $cd->{"args"}{"whitelist_modules"}B= grep {$_ eq $name;} @{$$cd{"args"}{"whitelist_modules"};}
ABresult
0X0
100
111
169 0 goto PASS;
170 }
171  
172 7653 ▶ if ($cd->{args}{pp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{"args"}{"pp"}) 80 7573 7653
173 80 ▶ if (!$known_modules{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $Data::Sah::Compiler::perl::known_modules{$name}) { } 0 80 80
elsif (not $Data::Sah::Compiler::perl::known_modules{$name}{'pp'}) { } 0 80 80
174 0 die "BUG: Haven't noted about Perl module '$name' as being pp/xs";
175 } elsif (!$known_modules{$name}{pp}) {
176 0 die "Use of XS module '$name' when compile option pp=1";
177 }
178 }
179  
180 7653 ▶ if ($cd->{args}{core}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{"args"}{"core"}) 40 7613 7653
181 40 ▶ if (!$known_modules{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $Data::Sah::Compiler::perl::known_modules{$name}) { } 0 40 40
elsif (not $Data::Sah::Compiler::perl::known_modules{$name}{'core'}) { } 0 40 40
182 0 die "BUG: Haven't noted about Perl module '$name' as being core/non-core";
183 } elsif (!$known_modules{$name}{core}) {
184 0 die "Use of non-core module '$name' when compile option core=1";
185 }
186 }
187  
188 7653 ▶ if ($cd->{args}{core_or_pp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{"args"}{"core_or_pp"}) 80 7573 7653
189 80 ▶ if (!$known_modules{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $Data::Sah::Compiler::perl::known_modules{$name}) { } 0 80 80
elsif (not $Data::Sah::Compiler::perl::known_modules{$name}{'pp'} || $Data::Sah::Compiler::perl::known_modules{$name}{'core'}) { } 0 80 80
190 0 die "BUG: Haven't noted about Perl module '$name' as being core/non-core or pp/xs";
191 ▶ } elsif (!$known_modules{$name}{pp} && !$known_modules{$name}{core}) {
Condition33%1/3
$Data::Sah::Compiler::perl::known_modules{$name}{'pp'} || $Data::Sah::Compiler::perl::known_modules{$name}{'core'}
l!l&&r!l&&!r
8000
MC/DC0%0/2
$Data::Sah::Compiler::perl::known_modules{$name}{'pp'} || $Data::Sah::Compiler::perl::known_modules{$name}{'core'}
$Data::Sah::Compiler::perl::known_modules{$name}{'pp'} $Data::Sah::Compiler::perl::known_modules{$name}{'core'}
Truth table33%1/3
A || B
A= $Data::Sah::Compiler::perl::known_modules{$name}{'pp'}B= $Data::Sah::Compiler::perl::known_modules{$name}{'core'}
ABresult
1X1
011
000
192 0 die "Use of non-core XS module '$name' when compile option core_or_pp=1";
193 }
194 }
195 }
196 PASS:
197 14100 $self->SUPER::add_module($cd, $name, $extra_keys, $allow_duplicate);
198 }
199  
200 17 ▶sub add_runtime_use {
Subroutine add_runtime_use: called CC=7 SCAR=19.7
Pod documented
201 17 my ($self, $cd, $name, $import_terms) = @_;
202 17 ▶ my $use_statement = "use $name".
Branch: 1 branch
BranchTrueFalseTotal
$import_terms && @$import_terms ? : 0 17 17
Condition33%1/3
$import_terms and @$import_terms
!ll&&!rl&&r
1700
MC/DC0%0/2
$import_terms and @$import_terms
$import_terms @$import_terms
Truth table33%1/3
A and B
A= $import_termsB= @$import_terms
ABresult
0X0
100
111
203 ($import_terms && @$import_terms ? " (".(join ",", @$import_terms).")" : "");
204  
205 # avoid duplicate use statement
206 17 for my $mod (@{ $cd->{modules} }) {
207 69 ▶ next unless $mod->{phase} eq 'runtime';
Branch: 1 branch
BranchTrueFalseTotal
unless $mod->{"phase"} eq "runtime" 30 39 69
208 return if $mod->{use_statement} &&
209 39 ▶ $mod->{use_statement} eq $use_statement;
Branch: 1 branch
BranchTrueFalseTotal
if $mod->{"use_statement"} and $mod->{"use_statement"} eq $use_statement 5 34 39
Condition100%3/3
$mod->{"use_statement"} and $mod->{"use_statement"} eq $use_statement
!ll&&!rl&&r
17175
MC/DC100%2/2
$mod->{"use_statement"} and $mod->{"use_statement"} eq $use_statement
$mod->{"use_statement"} $mod->{"use_statement"} eq $use_statement
Truth table100%3/3
A and B
A= $mod->{"use_statement"}B= $mod->{"use_statement"} eq $use_statement
ABresult
0X0
100
111
210 }
211  
212 $self->add_runtime_module(
213 12 $cd,
214 $name,
215 {
216 use_statement => $use_statement,
217 },
218 1, # allow duplicate
219 );
220 }
221  
222 5020 ▶sub add_runtime_no {
Subroutine add_runtime_no: called CC=7 SCAR=22.5
Pod documented
223 5020 my ($self, $cd, $name, $import_terms) = @_;
224  
225 5020 ▶ my $use_statement = "no $name".
Branch: 1 branch
BranchTrueFalseTotal
$import_terms && @$import_terms ? : 5020 0 5020
Condition33%1/3
$import_terms and @$import_terms
!ll&&!rl&&r
005020
MC/DC0%0/2
$import_terms and @$import_terms
$import_terms @$import_terms
Truth table33%1/3
A and B
A= $import_termsB= @$import_terms
ABresult
0X0
100
111
226 ($import_terms && @$import_terms ? " (".(join ",", @$import_terms).")" : "");
227  
228 # avoid duplicate use statement
229 5020 for my $mod (@{ $cd->{modules} }) {
230 330 ▶ next unless $mod->{phase} eq 'runtime';
Branch: 1 branch
BranchTrueFalseTotal
unless $mod->{"phase"} eq "runtime" 0 330 330
231 return if $mod->{use_statement} &&
232 330 ▶ $mod->{use_statement} eq $use_statement;
Branch: 1 branch
BranchTrueFalseTotal
if $mod->{"use_statement"} and $mod->{"use_statement"} eq $use_statement 330 0 330
Condition33%1/3
$mod->{"use_statement"} and $mod->{"use_statement"} eq $use_statement
!ll&&!rl&&r
00330
MC/DC0%0/2
$mod->{"use_statement"} and $mod->{"use_statement"} eq $use_statement
$mod->{"use_statement"} $mod->{"use_statement"} eq $use_statement
Truth table33%1/3
A and B
A= $mod->{"use_statement"}B= $mod->{"use_statement"} eq $use_statement
ABresult
0X0
100
111
233 }
234  
235 $self->add_runtime_module(
236 4690 $cd,
237 $name,
238 {
239 use_statement => $use_statement,
240 },
241 1, # allow duplicate
242 );
243 }
244  
245 # add Scalar::Util::Numeric module
246 1768 ▶sub add_sun_module {
Subroutine add_sun_module: called CC=5 SCAR=16.3
Pod documented
247 1768 my ($self, $cd) = @_;
248 1768 ▶ if ($cd->{args}{pp} || $cd->{args}{core_or_pp} ||
Branch: 2 branches
BranchTrueFalseTotal
if ($cd->{'args'}{'pp'} or $cd->{'args'}{'core_or_pp'} or not eval { do { require Scalar::Util::Numeric; 1 } }) { } 80 1688 1768
elsif ($cd->{'args'}{'core'}) { } 0 1688 1688
Condition83%5/6
$cd->{"args"}{"pp"} or $cd->{"args"}{"core_or_pp"} or not eval { do { require Scalar::Util::Numeric; 1 } }
l!l&&r!l&&!r
8001688
$cd->{"args"}{"pp"} or $cd->{"args"}{"core_or_pp"}
l!l&&r!l&&!r
40401688
MC/DC66%2/3
$cd->{"args"}{"pp"} or $cd->{"args"}{"core_or_pp"} or not eval { do { require Scalar::Util::Numeric; 1 } }
$cd->{"args"}{"pp"} $cd->{"args"}{"core_or_pp"} not eval { do { require Scalar::Util::Numeric; 1 } }
Truth table75%3/4
A or B or not(C)
A= $cd->{"args"}{"pp"}B= $cd->{"args"}{"core_or_pp"}C= not eval { do { require Scalar::Util::Numeric; 1 } }
ABCresult
1XX1
01X1
0011
0000
249 1688 !eval { require Scalar::Util::Numeric; 1 }) {
250 80 $cd->{_sun_module} = 'Scalar::Util::Numeric::PP';
251 } elsif ($cd->{args}{core}) {
252 # just to make sure compilation will fail if we mistakenly use a sun
253 # module
254 0 $cd->{_sun_module} = 'Foo';
255 } else {
256 1688 $cd->{_sun_module} = 'Scalar::Util::Numeric';
257 }
258 1768 $self->add_runtime_module($cd, $cd->{_sun_module});
259 }
260  
261 # evaluate all terms, then return the last term. user has to make sure all the
262 # terms are properly parenthesized if it contains operator with precedence less
263 # than the list operator.
264 1382 ▶sub expr_list {
Subroutine expr_list: called CC=1 SCAR=0.0
Pod undocumented
265 1382 my ($self, @t) = @_;
266 1382 "(".join(", ", @t).")";
267 }
268  
269 5115 ▶sub expr_defined {
Subroutine expr_defined: called CC=1 SCAR=0.0
Pod undocumented
270 5115 my ($self, $t) = @_;
271 5115 "defined($t)";
272 }
273  
274 9606 ▶sub expr_array {
Subroutine expr_array: called CC=1 SCAR=0.0
Pod undocumented
275 9606 my ($self, @t) = @_;
276 9606 "[".join(",", @t)."]";
277 }
278  
279 658 ▶sub expr_array_subscript {
Subroutine expr_array_subscript: called CC=1 SCAR=0.0
Pod undocumented
280 658 my ($self, $at, $idxt) = @_;
281 658 "$at->\[$idxt]";
282 }
283  
284 0 ▶sub expr_last_elem {
Subroutine expr_last_elem: not called CC=1 SCAR=6.9
Pod undocumented
285 0 my ($self, $at, $idxt) = @_;
286 0 "$at->\[-1]";
287 }
288  
289 86 ▶sub expr_push {
Subroutine expr_push: called CC=1 SCAR=0.0
Pod undocumented
290 86 my ($self, $at, $elt) = @_;
291 86 "push(\@{$at}, $elt)";
292 }
293  
294 612 ▶sub expr_pop {
Subroutine expr_pop: called CC=1 SCAR=0.0
Pod undocumented
295 612 my ($self, $at, $elt) = @_;
296 612 "pop(\@{$at})";
297 }
298  
299 86 ▶sub expr_push_and_pop_dpath_between_expr {
Subroutine expr_push_and_pop_dpath_between_expr: called CC=1 SCAR=0.0
Pod undocumented
300 86 my ($self, $et) = @_;
301 86 join(
302 "",
303 "[",
304 $self->expr_push('$_sahv_dpath', $self->literal(undef)), ", ", # 0
305 "scalar", $self->enclose_paren($et), ", ", #1 ('scalar' to avoid list flattening)
306 $self->expr_pop('$_sahv_dpath'), # 2
307 "]->[1]",
308 );
309 }
310  
311 4095 ▶sub expr_prefix_dpath {
Subroutine expr_prefix_dpath: called CC=1 SCAR=0.0
Pod undocumented
312 4095 my ($self, $t) = @_;
313 4095 '(@$_sahv_dpath ? \'@\'.join("",map {"[$_]"} @$_sahv_dpath).": " : "") . ' . $t;
314 }
315  
316 # $l = $r
317 1821 ▶sub expr_set {
Subroutine expr_set: called CC=1 SCAR=0.0
Pod undocumented
318 1821 my ($self, $l, $r) = @_;
319 1821 "($l = $r)";
320 }
321  
322 # $l //= $r
323 91 ▶sub expr_setif {
Subroutine expr_setif: called CC=1 SCAR=0.0
Pod undocumented
324 91 my ($self, $l, $r) = @_;
325 91 "($l //= $r)";
326 }
327  
328 13086 ▶sub expr_set_err_str {
Subroutine expr_set_err_str: called CC=1 SCAR=0.0
Pod undocumented
329 13086 my ($self, $et, $err_expr) = @_;
330 13086 "($et //= $err_expr)";
331 }
332  
333 3710 ▶sub expr_set_err_full {
Subroutine expr_set_err_full: called CC=1 SCAR=0.0
Pod undocumented
334 3710 my ($self, $et, $k, $err_expr) = @_;
335 3710 "($et\->{$k}{join('/',\@\$_sahv_dpath)} //= $err_expr)";
336 }
337  
338 202 ▶sub expr_reset_err_str {
Subroutine expr_reset_err_str: called CC=1 SCAR=0.0
Pod undocumented
339 202 my ($self, $et, $err_expr) = @_;
340 202 "($et = undef, 1)";
341 }
342  
343 202 ▶sub expr_reset_err_full {
Subroutine expr_reset_err_full: called CC=1 SCAR=0.0
Pod undocumented
344 202 my ($self, $et) = @_;
345 202 "(delete($et\->{errors}{join('/',\@\$_sahv_dpath)}), 1)";
346 }
347  
348 # $cond_term ? $true_term : $false_term
349 1468 ▶sub expr_ternary {
Subroutine expr_ternary: called CC=1 SCAR=0.0
Pod undocumented
350 1468 my ($self, $cond_term, $true_term, $false_term) = @_;
351 1468 "$cond_term ? $true_term : $false_term";
352 }
353  
354 0 ▶sub expr_log {
Subroutine expr_log: not called CC=1 SCAR=6.9
Pod undocumented
355 0 my ($self, $cd, @expr) = @_;
356  
357 "log_trace('[sah validator](spath=%s) %s', " .
358 0 $self->literal($cd->{spath}).", " . join(", ", @expr) . ")";
359 }
360  
361 # convert Expr expression to perl expression
362 17 ▶sub expr {
Subroutine expr: called CC=1 SCAR=0.0
Pod undocumented
363 17 require Language::Expr;
364  
365 17 my ($self, $cd, $expr) = @_;
366  
367 17 $self->add_runtime_use($cd, 'boolean');
368 17 "(" . Language::Expr->new->get_compiler('perl')->compile($expr) . ")";
369 }
370  
371 # wrap statements into an expression
372 5258 ▶sub expr_block {
Subroutine expr_block: called CC=1 SCAR=0.0
Pod undocumented
373 5258 my ($self, $code) = @_;
374 5258 join(
375 "",
376 "do {\n",
377 __indent(
378 $self->indent_character,
379 $code,
380 ),
381 "}",
382 );
383 }
384  
385 # whether block is implemented using function
386 607 ▶sub block_uses_sub { 0 }
Subroutine block_uses_sub: called CC=1 SCAR=0.0
Pod undocumented
387  
388 10943 ▶sub stmt_declare_local_var {
Subroutine stmt_declare_local_var: called CC=2 SCAR=6.9
Pod undocumented
389 10943 my ($self, $v, $vt) = @_;
390 10943 ▶ if ($vt eq 'undef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($vt eq 'undef') { } 1473 9470 10943
391 1473 "my \$$v;";
392 } else {
393 9470 "my \$$v = $vt;";
394 }
395 }
396  
397 4691 ▶sub expr_anon_sub {
Subroutine expr_anon_sub: called CC=1 SCAR=0.0
Pod undocumented
398 4691 my ($self, $args, $code) = @_;
399  
400 4691 join(
401 "",
402 "sub {\n",
403 __indent(
404 $self->indent_character,
405 join(
406 "",
407 ("my (".join(", ", @$args).") = \@_;\n") x !!@$args,
408 $code,
409 ),
410 ),
411 "}"
412 );
413 }
414  
415 # enclose $stmt in an eval/try block, return true if succeeds, false if error
416 # was thrown. XXX error message was not recorded yet.
417 0 ▶sub expr_eval {
Subroutine expr_eval: not called CC=1 SCAR=6.9
Pod undocumented
418 0 my ($self, $stmt) = @_;
419 0 "(eval { $stmt }, !\$@)";
420 }
421  
422 # Storable (fast, core) is not chosen because i cannot make it to dump 3 and "3"
423 # as "3". some other inconveniences (but not deal breaker): 1) only accepts
424 # references so we need to freeze \$foo or [$foo] instead of just $foo; 2) need
425 # to set $Storable::canonical to true, otherwise hash keys are not ordered.
426  
427 1226 ▶sub expr_dump {
Subroutine expr_dump: called CC=4 SCAR=19.7
Pod undocumented
428 1226 my ($self, $cd, $t) = @_;
429 1226 my $dump_module = $cd->{args}{dump_module};
430 1226 ▶ if ($dump_module eq 'Data::Dumper') {
Branch: 2 branches
BranchTrueFalseTotal
if ($dump_module eq 'Data::Dumper') { } 1226 0 1226
elsif ($dump_module eq 'Data::Dmp') { } 0 0 0
431 1226 "Data::Dumper->new([$t])->Terse(1)->Indent(0)->Sortkeys(1)->Dump";
432 } elsif ($dump_module eq 'Data::Dmp') {
433 0 "Data::Dmp::dmp($t)";
434 } else {
435 0 ▶ $self->_die($cd, "Unknown dump module '$dump_module'") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
436 }
437 }
438  
439 7185 ▶sub stmt_require_module {
Subroutine stmt_require_module: called CC=2 SCAR=6.9
Pod undocumented
440 7185 my ($self, $mod_record) = @_;
441  
442 7185 ▶ if ($mod_record->{use_statement}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod_record->{'use_statement'}) { } 4663 2522 7185
443 4663 return "$mod_record->{use_statement};";
444 } else {
445 2522 "require $mod_record->{name};";
446 }
447 }
448  
449 4691 ▶sub stmt_require_log_module {
Subroutine stmt_require_log_module: called CC=1 SCAR=0.0
Pod undocumented
450 4691 my ($self) = @_;
451 4691 'use Log::ger;';
452 }
453  
454 4691 ▶sub stmt_assign_hash_value {
Subroutine stmt_assign_hash_value: called CC=1 SCAR=0.0
Pod undocumented
455 4691 my ($self, $ht, $kt, $vt) = @_;
456 4691 "$ht\->{$kt} = $vt;";
457 }
458  
459 23455 ▶sub stmt_return {
Subroutine stmt_return: called CC=2 SCAR=7.6
Pod undocumented
460 23455 my $self = shift;
461 23455 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 23455 0 23455
462 23455 "return($_[0]);";
463 } else {
464 0 'return;';
465 }
466 }
467  
468 # currently unused
469 0 ▶sub expr_refer_or_call_sub {
Subroutine expr_refer_or_call_sub: not called CC=1 SCAR=6.9
Pod undocumented
470 0 my ($self, $name) = @_;
471 0 "do { no strict 'refs'; \\&{" . $self->literal($name) . "} }";
472 }
473  
474 0 ▶sub expr_call_sub {
Subroutine expr_call_sub: not called CC=1 SCAR=6.9
Pod undocumented
475 0 my ($self, $name, $args) = @_;
476 0 "$name(".join(", ", @$args).")";
477 }
478  
479 0 ▶sub expr_call_cached_validator {
Subroutine expr_call_cached_validator: not called CC=1 SCAR=6.9
Pod undocumented
480 0 my ($self, $cd, $schema_name) = @_;
481 0 my $subname = $self->cached_validator_subname($cd, $schema_name);
482 0 $self->expr_call_sub($subname, [$cd->{data_term}]);
483 }
484  
485 4724 ▶sub expr_validator_sub {
Subroutine expr_validator_sub: called CC=2 SCAR=6.9
Pod undocumented
486 4724 my ($self, %args) = @_;
487  
488 4724 $self->check_compile_args(\%args);
489  
490 4724 my $aref = delete $args{accept_ref};
491 4724 ▶ if ($aref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aref) { } 6 4718 4724
492 6 $args{var_term} = '$ref_'.$args{data_name};
493 6 $args{data_term} = '$$ref_'.$args{data_name};
494 } else {
495 4718 $args{var_term} = '$'.$args{data_name};
496 4718 $args{data_term} = '$'.$args{data_name};
497 }
498  
499 4724 $self->SUPER::expr_validator_sub(%args);
500 }
501  
502 222 ▶sub _str2reliteral {
Subroutine _str2reliteral: called CC=3 SCAR=11.0
503 222 require Regexp::Stringify;
504  
505 222 my ($self, $cd, $str) = @_;
506  
507 222 my $re;
508 222 ▶ if (ref($str) eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $str eq 'Regexp') { } 2 220 222
509 2 $re = $str;
510 } else {
511 220 eval { $re = qr/$str/ };
512 220 ▶ $self->_die($cd, "Invalid regex $str: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 217 220
513 }
514  
515 219 Regexp::Stringify::stringify_regexp(regexp=>$re, plver=>5.010);
516 }
517  
518 # check if sub named $name is defined and return true if it's the case
519 0 ▶sub sub_defined {
Subroutine sub_defined: not called CC=1 SCAR=6.9
Pod undocumented
520 0 my ($self, $name) = @_;
521 0 defined &{$name};
522 }
523  
524 0 ▶sub cached_validator_subname {
Subroutine cached_validator_subname: not called CC=2 SCAR=17.9
Pod undocumented
525 0 my ($self, $cd, $schema_name) = @_;
526 0 local $cd->{args}{return_type} = 'bool_valid'; # XXX temp
527 0 ▶ die unless $cd->{args}{return_type} =~ /\A(bool|str|hash)_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cd->{"args"}{"return_type"} =~ /\A(bool|str|hash)_/ 0 0 0
528 0 "Data::Sah::_GeneratedValidators::Returns".ucfirst($1)."::".$schema_name;
529 }
530  
531 0 ▶sub gen_cached_validator {
Subroutine gen_cached_validator: not called CC=3 SCAR=24.8
Pod undocumented
532 0 my ($self, $cd, $schema_name) = @_;
533 0 my $subname = $self->cached_validator_subname($cd, $schema_name);
534 0 ▶ return if defined &{$subname};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{$subname;} 0 0 0
535 0 log_trace "Generating cached validator for base schema %s", $schema_name;
536 my $sub_code = $self->expr_validator_sub(
537 0 %{$cd->{args}},
538 schema => $schema_name,
539 schema_is_normalized => 0,
540 data_name => "data",
541 resolve_opts=>{allow_base_with_no_additional_clauses=>0},
542 return_type => "bool_valid", # XXX temp
543 );
544 0 my $code = "*$subname = $sub_code;";
545 0 eval $code; ## no critic: BuiltinFunctions::ProhibitStringyEval
546 0 ▶ $self->_die($cd, "Cannot generate cached validator for '$schema_name': $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
547 }
548  
549 23 1;
550 # ABSTRACT: Compile Sah schema to Perl code
551  
552 __END__
« Data/Sah/Human.pm ↑ Summary Data/Sah/Compiler/perl/TH/float.pm »