Sah/Schema/str_or_re_or_code.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 1 / 3 Podpod n/a totaltotal 62.5% 10 / 16 CC 2 SCAR 13.2
CC
2
Cov
69%
__ANON__
6.0
__ANON__
2.0
BEGIN
1.0
CRAP 3.8
« Sah/Schema/str_or_code.pm ↑ Summary Data/Sah/Filter/perl/Str/maybe_eval.pm »
1 package Sah::Schema::str_or_re_or_code;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
6 1 our $DATE = '2024-02-06'; # DATE
7 1 our $DIST = 'Sah-Schemas-Str'; # DIST
8 1 our $VERSION = '0.018'; # VERSION
9  
10 our $schema = [any => {
11 summary => 'String, or regex (if string is of the form `/.../`), or coderef (if string is in the form of `sub { ... }`)',
12 description => <<'_',
13  
14 Either string, Regexp object, or coderef is accepted.
15  
16 If string is of the form of `/.../` or `qr(...)`, then it will be compiled into
17 a Regexp object. If the regex pattern inside `/.../` or `qr(...)` is invalid,
18 value will be rejected. Currently, unlike in normal Perl, for the `qr(...)`
19 form, only parentheses `(` and `)` are allowed as the delimiter. Currently
20 modifiers `i`, `m`, and `s` after the second `/` are allowed.
21  
22 If string matches the regex `qr/\Asub\s*\{.*\}\z/s`, then it will be eval'ed
23 into a coderef. If the code fails to compile, the value will be rejected. Note
24 that this means you accept arbitrary code from the user to execute! Please make
25 sure first and foremost that this is acceptable in your case. Currently string
26 is eval'ed in the `main` package, without `use strict` or `use warnings`.
27  
28 This schema is handy if you want to accept string or regex or coderef from the
29 command-line.
30  
31 _
32 of => [
33 ['str'],
34 ['re'],
35 ['code'],
36 ],
37  
38 prefilters => [
39 'Str::maybe_convert_to_re',
40 'Str::maybe_eval',
41 ],
42  
43 examples => [
44 {value=>'', valid=>1},
45 {value=>'a', valid=>1},
46 {value=>{}, valid=>0, summary=>'Not a string'},
47  
48 # re
49 {value=>'//', valid=>1, validated_value=>qr//},
50 {value=>'/foo', valid=>1, summary=>'Becomes a string'},
51 {value=>'qr(foo', valid=>1, summary=>'Becomes a string'},
52 {value=>'qr(foo(', valid=>1, summary=>'Becomes a string'},
53 {value=>'qr/foo/', valid=>1, summary=>'Becomes a string'},
54  
55 {value=>'/foo.*/', valid=>1, validated_value=>qr/foo.*/},
56 {value=>'qr(foo.*)', valid=>1, validated_value=>qr/foo.*/},
57 {value=>'/foo/is', valid=>1, validated_value=>qr/foo/is},
58 {value=>'qr(foo)is', valid=>1, validated_value=>qr/foo/is},
59  
60 {value=>'/foo[/', valid=>0, summary=>'Invalid regex'},
61  
62 # code
63 0 ▶ {value=>'sub {}', valid=>1, code_validate=>sub { ref($_[0]) eq 'CODE' & !defined($_[0]->()) }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 ▶ {value=>'sub{"foo"}', valid=>1, code_validate=>sub { ref($_[0]) eq 'CODE' && $_[0]->() eq 'foo' }},
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "CODE" and $_[0]->() eq "foo" 0 0 0
65 1 {value=>'sub {', valid=>1, summary=>'Becomes a string'},
66  
67 {value=>'sub {1=2}', valid=>0, summary=>'Code does not compile'},
68 ],
69  
70 }];
71  
72 1 1;
73 # ABSTRACT:
74  
75 __END__
« Sah/Schema/str_or_code.pm ↑ Summary Data/Sah/Filter/perl/Str/maybe_eval.pm »