File Coverage

blib/lib/Regexp/From/String.pm
Criterion Covered Total %
statement 28 30 93.3
branch 19 20 95.0
condition 3 3 100.0
subroutine 5 5 100.0
pod 2 2 100.0
total 57 60 95.0


line stmt bran cond sub pod time code
1              
2             use strict;
3 1     1   106635 use warnings;
  1         12  
  1         29  
4 1     1   4  
  1         3  
  1         31  
5             use Exporter 'import';
6 1     1   4  
  1         2  
  1         468  
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2022-07-09'; # DATE
9             our $DIST = 'Regexp-From-String'; # DIST
10             our $VERSION = '0.003'; # VERSION
11              
12             our @EXPORT_OK = qw(str_maybe_to_re str_to_re);
13              
14             my $str = shift;
15             if ($str =~ m!\A(?:/.*/|qr\(.*\))(?:[ims]*)\z!s) {
16 8     8 1 1590 my $re = eval(substr($str, 0, 2) eq 'qr' ? $str : "qr$str"); ## no critic: BuiltinFunctions::ProhibitStringyEval
17 8 100       56 die if $@;
18 3 100       190 return $re;
19 3 100       22 }
20 2         17 $str;
21             }
22 5         42  
23             my $opts = ref $_[0] eq 'HASH' ? shift : {};
24             my $str = shift;
25             if (!$opts->{always_quote} && $str =~ m!\A(?:/.*/|qr\(.*\))(?:[ims]*)\z!s) {
26 9 100   9 1 3685 my $re = eval(substr($str, 0, 2) eq 'qr' ? $str : "qr$str"); ## no critic: BuiltinFunctions::ProhibitStringyEval
27 9         22 die if $@;
28 9 100 100     68 return $re;
29 3 100       175 } else {
30 3 100       21 $str = quotemeta($str);
31 2         14 if ($opts->{anchored}) {
32             if ($opts->{case_insensitive}) { return qr/\A$str\z/i } else { return qr/\A$str\z/ }
33 6         14 } else {
34 6 100       14 if ($opts->{case_insensitive}) { return qr/$str/i } else { return qr/$str/ }
35 1 50       3 }
  0         0  
  1         20  
36             }
37 5 100       10 $str;
  1         14  
  4         95  
38             }
39              
40 0           1;
41             # ABSTRACT: Convert '/.../' or 'qr(...)' into Regexp object
42              
43              
44             =pod
45              
46             =encoding UTF-8
47              
48             =head1 NAME
49              
50             Regexp::From::String - Convert '/.../' or 'qr(...)' into Regexp object
51              
52             =head1 VERSION
53              
54             This document describes version 0.003 of Regexp::From::String (from Perl distribution Regexp-From-String), released on 2022-07-09.
55              
56             =head1 SYNOPSIS
57              
58             use Regexp::From::String qw(str_maybe_to_re str_to_re);
59              
60             my $re1 = str_maybe_to_re('foo'); # stays as string 'foo'
61             my $re2 = str_maybe_to_re('/foo'); # stays as string '/foo'
62             my $re3 = str_maybe_to_re('/foo/'); # compiled to Regexp object qr(foo)
63             my $re4 = str_maybe_to_re('qr(foo)i'); # compiled to Regexp object qr(foo)i
64             my $re5 = str_maybe_to_re('qr(foo[)i'); # dies, invalid regex syntax
65              
66             my $re1 = str_to_re('foo'); # compiled to Regexp object qr(foo)
67             my $re2 = str_to_re('/foo'); # compiled to Regexp object qr(/foo)
68             my $re2 = str_to_re({case_insensitive=>1}, 'foo[]'); # compiled to Regexp object qr(foo\[\])i
69             my $re2 = str_to_re({anchored=>1}, 'foo[]'); # compiled to Regexp object qr(\Afoo\[\]\z)
70             my $re3 = str_to_re('/foo/'); # compiled to Regexp object qr(foo)
71             my $re4 = str_to_re('qr(foo)i'); # compiled to Regexp object qr(foo)i
72             my $re4 = str_to_re('qr(foo.)'); # compiled to Regexp object qr(foo.)
73             my $re4 = str_to_re({always_quote=>1}, 'qr(foo.)'); # compiled to Regexp object qr(qr\(foo\.\))
74             my $re5 = str_to_re('qr(foo[)i'); # dies, invalid regex syntax
75              
76             =head1 FUNCTIONS
77              
78             =head2 str_maybe_to_re
79              
80             Maybe convert string to Regexp object.
81              
82             Usage:
83              
84             $str_or_re = str_maybe_to_re($str);
85              
86             Check if string C<$str> is in the form of C</.../> or C<qr(...)'> and if so,
87             compile the inside regex (currently simply using stringy C<eval>) and return the
88             resulting Regexp object. Otherwise, will simply return the argument unmodified.
89              
90             Will die if compilation fails, e.g. when the regexp syntax is invalid.
91              
92             For the C<qr(...)> form, unlike in Perl, currently only the C<()> delimiter
93             characters are recognized and not others.
94              
95             Optional modifiers C<i>, C<m>, and C<s> are currently allowed at the end.
96              
97             =head2 str_to_re
98              
99             Convert string to Regexp object.
100              
101             Usage:
102              
103             $str_or_re = str_to_re([ \%opts , ] $str);
104              
105             This function is similar to L</str_maybe_to_re> except that when string is not
106             in the form of C</.../> or C<qr(...)>, the string is C<quotemeta()>'ed then
107             converted to a Regexp object anyway. There are some options available to specify
108             in first argument hashref C<\%opts>:
109              
110             =over
111              
112             =item * always_quote
113              
114             Bool. If set to true, will always C<quotemeta()> regardless of whether the
115             string is in the form of C</.../> or C<qr(...)> or not. This means user will not
116             be able to use metacharacters and the Regexp will only match the literal string
117             (with some option like anchoring and case-sensitivity, see other options).
118              
119             =item * case_insensitive
120              
121             Bool. If set to true will compile to Regexp object with C<i> regexp modifier.
122              
123             =item * anchored
124              
125             Bool. If set to true will anchor the pattern with C<\A> and C<\z>.
126              
127             =back
128              
129             =head1 HOMEPAGE
130              
131             Please visit the project's homepage at L<https://metacpan.org/release/Regexp-From-String>.
132              
133             =head1 SOURCE
134              
135             Source repository is at L<https://github.com/perlancar/perl-Regexp-From-String>.
136              
137             =head1 SEE ALSO
138              
139             L<Sah::Schema::str_or_re>
140              
141             =head1 AUTHOR
142              
143             perlancar <perlancar@cpan.org>
144              
145             =head1 CONTRIBUTING
146              
147              
148             To contribute, you can send patches by email/via RT, or send pull requests on
149             GitHub.
150              
151             Most of the time, you don't need to build the distribution yourself. You can
152             simply modify the code, then test via:
153              
154             % prove -l
155              
156             If you want to build the distribution (e.g. to try to install it locally on your
157             system), you can install L<Dist::Zilla>,
158             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
159             Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required
160             beyond that are considered a bug and can be reported to me.
161              
162             =head1 COPYRIGHT AND LICENSE
163              
164             This software is copyright (c) 2022 by perlancar <perlancar@cpan.org>.
165              
166             This is free software; you can redistribute it and/or modify it under
167             the same terms as the Perl 5 programming language system itself.
168              
169             =head1 BUGS
170              
171             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Regexp-From-String>
172              
173             When submitting a bug or request, please include a test-file or a
174             patch to an existing test-file that illustrates the bug or desired
175             feature.
176              
177             =cut