| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Sah::Schema::re_or_code_from_str; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
507735
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
591
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
|
6
|
|
|
|
|
|
|
our $DATE = '2023-12-20'; # DATE |
|
7
|
|
|
|
|
|
|
our $DIST = 'Sah-Schemas-Re'; # DIST |
|
8
|
|
|
|
|
|
|
our $VERSION = '0.006'; # VERSION |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $schema = [any => { |
|
11
|
|
|
|
|
|
|
summary => 'Regex (convertable from string of the form `/.../`) or coderef (convertable from string of the form `sub { ... }`)', |
|
12
|
|
|
|
|
|
|
description => <<'_', |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Either Regexp object or coderef is accepted. |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Coercion from string for Regexp is available if string is of the form of `/.../` |
|
17
|
|
|
|
|
|
|
or `qr(...)`; it will be compiled into a Regexp object. If the regex pattern |
|
18
|
|
|
|
|
|
|
inside `/.../` or `qr(...)` is invalid, value will be rejected. Currently, |
|
19
|
|
|
|
|
|
|
unlike in normal Perl, for the `qr(...)` form, only parentheses `(` and `)` are |
|
20
|
|
|
|
|
|
|
allowed as the delimiter. Currently modifiers `i`, `m`, and `s` after the second |
|
21
|
|
|
|
|
|
|
`/` are allowed. |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Coercion from string for coderef is available if string matches the regex |
|
24
|
|
|
|
|
|
|
`qr/\Asub\s*\{.*\}\z/s`, then it will be eval'ed into a coderef. If the code |
|
25
|
|
|
|
|
|
|
fails to compile, the value will be rejected. Note that this means you accept |
|
26
|
|
|
|
|
|
|
arbitrary code from the user to execute! Please make sure first and foremost |
|
27
|
|
|
|
|
|
|
that this is acceptable in your case. Currently string is eval'ed in the `main` |
|
28
|
|
|
|
|
|
|
package, without `use strict` or `use warnings`. |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
Unlike the default behavior of the `re` Sah type, coercion from other string not |
|
31
|
|
|
|
|
|
|
in the form of `/.../` or `qr(...)` is not available. Thus, such values will be |
|
32
|
|
|
|
|
|
|
rejected. |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
This schema is handy if you want to accept regex or coderef from the |
|
35
|
|
|
|
|
|
|
command-line. |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
_ |
|
38
|
|
|
|
|
|
|
of => [ |
|
39
|
|
|
|
|
|
|
['obj::re'], |
|
40
|
|
|
|
|
|
|
['code'], |
|
41
|
|
|
|
|
|
|
], |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
prefilters => [ |
|
44
|
|
|
|
|
|
|
'Str::maybe_convert_to_re', |
|
45
|
|
|
|
|
|
|
'Str::maybe_eval', |
|
46
|
|
|
|
|
|
|
], |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
examples => [ |
|
49
|
|
|
|
|
|
|
{value=>'', valid=>0, summary=>'Not to regex or code'}, |
|
50
|
|
|
|
|
|
|
{value=>'a', valid=>0, summary=>'Not a regex or code'}, |
|
51
|
|
|
|
|
|
|
{value=>{}, valid=>0, summary=>'Not a regex or code'}, |
|
52
|
|
|
|
|
|
|
{value=>qr//, valid=>1}, |
|
53
|
|
|
|
|
|
|
{value=>sub{}, valid=>1}, |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# re |
|
56
|
|
|
|
|
|
|
{value=>'//', valid=>1, validated_value=>qr//}, |
|
57
|
|
|
|
|
|
|
{value=>'/foo', valid=>0, summary=>'Not converted to regex'}, |
|
58
|
|
|
|
|
|
|
{value=>'qr(foo', valid=>0, summary=>'Not converted to regex'}, |
|
59
|
|
|
|
|
|
|
{value=>'qr(foo(', valid=>0, summary=>'Not converted to regex'}, |
|
60
|
|
|
|
|
|
|
{value=>'qr/foo/', valid=>0, summary=>'Not converted to regex'}, |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
{value=>'/foo.*/', valid=>1, validated_value=>qr/foo.*/}, |
|
63
|
|
|
|
|
|
|
{value=>'qr(foo.*)', valid=>1, validated_value=>qr/foo.*/}, |
|
64
|
|
|
|
|
|
|
{value=>'/foo/is', valid=>1, validated_value=>qr/foo/is}, |
|
65
|
|
|
|
|
|
|
{value=>'qr(foo)is', valid=>1, validated_value=>qr/foo/is}, |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
{value=>'/foo[/', valid=>0, summary=>'Invalid regex'}, |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# code |
|
70
|
|
|
|
|
|
|
{value=>'sub {}', valid=>1, code_validate=>sub { ref($_[0]) eq 'CODE' & !defined($_[0]->()) }}, |
|
71
|
|
|
|
|
|
|
{value=>'sub{"foo"}', valid=>1, code_validate=>sub { ref($_[0]) eq 'CODE' && $_[0]->() eq 'foo' }}, |
|
72
|
|
|
|
|
|
|
{value=>'sub {', valid=>0, summary=>'Not converted to code'}, |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
{value=>'sub {1=2}', valid=>0, summary=>'Code does not compile'}, |
|
75
|
|
|
|
|
|
|
], |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
}]; |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
|
80
|
|
|
|
|
|
|
# ABSTRACT: Regex (convertable from string of the form `/.../`) or coderef (convertable from string of the form `sub { ... }`) |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
__END__ |