line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Regexp::Stringify; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $DATE = '2015-09-04'; # DATE |
4
|
|
|
|
|
|
|
our $VERSION = '0.04'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
19899
|
use 5.010001; |
|
1
|
|
|
|
|
3
|
|
7
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
20
|
|
8
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use re qw(regexp_pattern); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
263
|
|
11
|
1
|
|
|
1
|
|
677
|
use Version::Util qw(version_ge); |
|
1
|
|
|
|
|
3104
|
|
|
1
|
|
|
|
|
88
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
5
|
use Exporter; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
328
|
|
14
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
15
|
|
|
|
|
|
|
our @EXPORT_OK = qw(stringify_regexp); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our %SPEC; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$SPEC{stringify_regexp} = { |
20
|
|
|
|
|
|
|
v => 1.1, |
21
|
|
|
|
|
|
|
summary => 'Stringify a Regexp object', |
22
|
|
|
|
|
|
|
description => <<'_', |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
This routine is an alternative to Perl's default stringification of Regexp |
25
|
|
|
|
|
|
|
object (i.e.:`"$re"`) and has some features/options, e.g.: producing regexp |
26
|
|
|
|
|
|
|
string that is compatible with certain perl versions. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
If given a string (or other non-Regexp object), will return it as-is. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
_ |
31
|
|
|
|
|
|
|
args => { |
32
|
|
|
|
|
|
|
regexp => { |
33
|
|
|
|
|
|
|
schema => 're*', |
34
|
|
|
|
|
|
|
req => 1, |
35
|
|
|
|
|
|
|
pos => 0, |
36
|
|
|
|
|
|
|
}, |
37
|
|
|
|
|
|
|
plver => { |
38
|
|
|
|
|
|
|
summary => 'Target perl version', |
39
|
|
|
|
|
|
|
schema => 'str*', |
40
|
|
|
|
|
|
|
description => <<'_', |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Try to produce a regexp object compatible with a certain perl version (should at |
43
|
|
|
|
|
|
|
least be >= 5.10). |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
For example, in perl 5.14 regex stringification changes, e.g. `qr/hlagh/i` would |
46
|
|
|
|
|
|
|
previously be stringified as `(?i-xsm:hlagh)`, but now it's stringified as |
47
|
|
|
|
|
|
|
`(?^i:hlagh)`. If you set `plver` to 5.10 or 5.12, then this routine will |
48
|
|
|
|
|
|
|
still produce the former. It will also ignore regexp modifiers that are |
49
|
|
|
|
|
|
|
introduced in newer perls. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Note that not all regexp objects are translatable to older perls, e.g. if they |
52
|
|
|
|
|
|
|
contain constructs not known to older perls like `(^...)` before perl 5.14. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
_ |
55
|
|
|
|
|
|
|
}, |
56
|
|
|
|
|
|
|
with_qr => { |
57
|
|
|
|
|
|
|
schema => 'bool', |
58
|
|
|
|
|
|
|
description => <<'_', |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
If you set this to 1, then `qr/a/i` will be stringified as `'qr/a/i'` instead as |
61
|
|
|
|
|
|
|
`'(^i:a)'`. The resulting string can then be eval-ed to recreate the Regexp |
62
|
|
|
|
|
|
|
object. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
_ |
65
|
|
|
|
|
|
|
}, |
66
|
|
|
|
|
|
|
}, |
67
|
|
|
|
|
|
|
result_naked => 1, |
68
|
|
|
|
|
|
|
result => { |
69
|
|
|
|
|
|
|
schema => 'str*', |
70
|
|
|
|
|
|
|
}, |
71
|
|
|
|
|
|
|
}; |
72
|
|
|
|
|
|
|
sub stringify_regexp { |
73
|
7
|
|
|
7
|
1
|
37
|
my %args = @_; |
74
|
|
|
|
|
|
|
|
75
|
7
|
|
|
|
|
12
|
my $re = $args{regexp}; |
76
|
7
|
50
|
|
|
|
19
|
return $re unless ref($re) eq 'Regexp'; |
77
|
7
|
|
33
|
|
|
17
|
my $plver = $args{plver} // $^V; |
78
|
|
|
|
|
|
|
|
79
|
7
|
|
|
|
|
22
|
my ($pat, $mod) = regexp_pattern($re); |
80
|
|
|
|
|
|
|
|
81
|
7
|
|
|
|
|
19
|
my $ge_5140 = version_ge($plver, 5.014); |
82
|
7
|
100
|
|
|
|
149
|
unless ($ge_5140) { |
83
|
2
|
|
|
|
|
5
|
$mod =~ s/[adlu]//g; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
7
|
100
|
|
|
|
18
|
if ($args{with_qr}) { |
87
|
2
|
|
|
|
|
14
|
return "qr($pat)$mod"; |
88
|
|
|
|
|
|
|
} else { |
89
|
5
|
100
|
|
|
|
11
|
if ($ge_5140) { |
90
|
3
|
|
|
|
|
22
|
return "(^$mod:$pat)"; |
91
|
|
|
|
|
|
|
} else { |
92
|
2
|
|
|
|
|
12
|
return "(?:(?$mod-)$pat)"; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1; |
98
|
|
|
|
|
|
|
# ABSTRACT: Stringify a Regexp object |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
__END__ |