/usr/share/perl5/Regexp/Stringify.pm is in libregexp-stringify-perl 0.04-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | package Regexp::Stringify;
our $DATE = '2015-09-04'; # DATE
our $VERSION = '0.04'; # VERSION
use 5.010001;
use strict;
use warnings;
use re qw(regexp_pattern);
use Version::Util qw(version_ge);
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(stringify_regexp);
our %SPEC;
$SPEC{stringify_regexp} = {
v => 1.1,
summary => 'Stringify a Regexp object',
description => <<'_',
This routine is an alternative to Perl's default stringification of Regexp
object (i.e.:`"$re"`) and has some features/options, e.g.: producing regexp
string that is compatible with certain perl versions.
If given a string (or other non-Regexp object), will return it as-is.
_
args => {
regexp => {
schema => 're*',
req => 1,
pos => 0,
},
plver => {
summary => 'Target perl version',
schema => 'str*',
description => <<'_',
Try to produce a regexp object compatible with a certain perl version (should at
least be >= 5.10).
For example, in perl 5.14 regex stringification changes, e.g. `qr/hlagh/i` would
previously be stringified as `(?i-xsm:hlagh)`, but now it's stringified as
`(?^i:hlagh)`. If you set `plver` to 5.10 or 5.12, then this routine will
still produce the former. It will also ignore regexp modifiers that are
introduced in newer perls.
Note that not all regexp objects are translatable to older perls, e.g. if they
contain constructs not known to older perls like `(^...)` before perl 5.14.
_
},
with_qr => {
schema => 'bool',
description => <<'_',
If you set this to 1, then `qr/a/i` will be stringified as `'qr/a/i'` instead as
`'(^i:a)'`. The resulting string can then be eval-ed to recreate the Regexp
object.
_
},
},
result_naked => 1,
result => {
schema => 'str*',
},
};
sub stringify_regexp {
my %args = @_;
my $re = $args{regexp};
return $re unless ref($re) eq 'Regexp';
my $plver = $args{plver} // $^V;
my ($pat, $mod) = regexp_pattern($re);
my $ge_5140 = version_ge($plver, 5.014);
unless ($ge_5140) {
$mod =~ s/[adlu]//g;
}
if ($args{with_qr}) {
return "qr($pat)$mod";
} else {
if ($ge_5140) {
return "(^$mod:$pat)";
} else {
return "(?:(?$mod-)$pat)";
}
}
}
1;
# ABSTRACT: Stringify a Regexp object
__END__
=pod
=encoding UTF-8
=head1 NAME
Regexp::Stringify - Stringify a Regexp object
=head1 VERSION
This document describes version 0.04 of Regexp::Stringify (from Perl distribution Regexp-Stringify), released on 2015-09-04.
=head1 SYNOPSIS
Assuming this runs on Perl 5.14 or newer.
use Regexp::Stringify qw(stringify_regexp);
$str = stringify_regexp(regexp=>qr/a/i); # '(^i:a)'
$str = stringify_regexp(regexp=>qr/a/i, with_qr=>1); # 'qr(a)i'
$str = stringify_regexp(regexp=>qr/a/i, plver=>5.010); # '(?:(?i-)a)'
$str = stringify_regexp(regexp=>qr/a/ui, plver=>5.010); # '(?:(?i-)a)'
=head1 FUNCTIONS
=head2 stringify_regexp(%args) -> str
Stringify a Regexp object.
This routine is an alternative to Perl's default stringification of Regexp
object (i.e.:C<"$re">) and has some features/options, e.g.: producing regexp
string that is compatible with certain perl versions.
If given a string (or other non-Regexp object), will return it as-is.
Arguments ('*' denotes required arguments):
=over 4
=item * B<plver> => I<str>
Target perl version.
Try to produce a regexp object compatible with a certain perl version (should at
least be >= 5.10).
For example, in perl 5.14 regex stringification changes, e.g. C<qr/hlagh/i> would
previously be stringified as C<(?i-xsm:hlagh)>, but now it's stringified as
C<(?^i:hlagh)>. If you set C<plver> to 5.10 or 5.12, then this routine will
still produce the former. It will also ignore regexp modifiers that are
introduced in newer perls.
Note that not all regexp objects are translatable to older perls, e.g. if they
contain constructs not known to older perls like C<(^...)> before perl 5.14.
=item * B<regexp>* => I<re>
=item * B<with_qr> => I<bool>
If you set this to 1, then C<qr/a/i> will be stringified as C<'qr/a/i'> instead as
C<'(^i:a)'>. The resulting string can then be eval-ed to recreate the Regexp
object.
=back
Return value: (str)
=head1 HOMEPAGE
Please visit the project's homepage at L<https://metacpan.org/release/Regexp-Stringify>.
=head1 SOURCE
Source repository is at L<https://github.com/perlancar/perl-Regexp-Stringify>.
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Regexp-Stringify>
When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.
=head1 AUTHOR
perlancar <perlancar@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|