File Coverage

blib/lib/String/Query/To/Regexp.pm
Criterion Covered Total %
statement 34 34 100.0
branch 20 22 90.9
condition 2 2 100.0
subroutine 5 5 100.0
pod 1 1 100.0
total 62 64 96.8


line stmt bran cond sub pod time code
1              
2             use 5.010001;
3 1     1   105582 use strict;
  1         14  
4 1     1   7 use warnings;
  1         2  
  1         37  
5 1     1   6  
  1         1  
  1         31  
6             use Exporter 'import';
7 1     1   5 our @EXPORT_OK = qw(query2re);
  1         3  
  1         466  
8              
9             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10             our $DATE = '2022-11-02'; # DATE
11             our $DIST = 'String-Query-To-Regexp'; # DIST
12             our $VERSION = '0.002'; # VERSION
13              
14             my $opts = ref($_[0]) eq 'HASH' ? {%{shift()}} : {};
15             my $bool = delete $opts->{bool} // 'and';
16 11 100   11 1 18708 my $ci = delete $opts->{ci};
  7         22  
17 11   100     47 my $word = delete $opts->{word};
18 11         18 my $opt_re = delete $opts->{re};
19 11         18 die "query2re(): Unknown option(s): ".
20 11         16 join(", ", sort keys %$opts) if keys %$opts;
21 11 100       62  
22             return qr// unless @_;
23             my @re_parts;
24 10 50       26 for my $query0 (@_) {
25 10         16 my ($neg, $query) = $query0 =~ /\A(-?)(.*)/;
26 10         20  
27 17         83 if ($opt_re) {
28             if (ref $query0 eq 'Regexp') {
29 17 100       38 $query = $query0;
30 4 100       16 } else {
31 1         2 require Regexp::From::String;
32             $query = Regexp::From::String::str_maybe_to_re($query);
33 3         595 $query = quotemeta($query) unless ref $query eq 'Regexp';
34 3         583 }
35 3 100       103 } else {
36             $query = quotemeta $query;
37             }
38 13         23  
39             if ($word) {
40             push @re_parts, $neg ? "(?!.*\\b$query\\b)" : "(?=.*\\b$query\\b)";
41 17 100       31 } else {
42 1 50       6 push @re_parts, $neg ? "(?!.*$query)" : "(?=.*$query)";
43             }
44 16 100       55 }
45             my $re = $bool eq 'or' ? "(?:".join("|", @re_parts).")" : join("", @re_parts);
46             return $ci ? qr/\A$re.*\z/is : qr/\A$re.*\z/s;
47 10 100       33 }
48 10 100       218  
49             1;
50             # ABSTRACT: Convert query to regular expression
51              
52              
53             =pod
54              
55             =encoding UTF-8
56              
57             =head1 NAME
58              
59             String::Query::To::Regexp - Convert query to regular expression
60              
61             =head1 VERSION
62              
63             This document describes version 0.002 of String::Query::To::Regexp (from Perl distribution String-Query-To-Regexp), released on 2022-11-02.
64              
65             =head1 SYNOPSIS
66              
67             use String::Query::To::Regexp qw(query2re);
68              
69             my $re;
70              
71             $re = query2re("foo"); # => qr/\A(?=.*foo).*\z/s -> string must contain 'foo'
72             $re = query2re({ci=>1}, "foo"; # => qr/\A(?=.*foo).*\z/is -> string must contain 'foo', case-insensitively
73             $re = query2re("foo", "bar"); # => qr/\A(?=.*foo)(?=.*bar).*\z/s -> string must contain 'foo' and 'bar', order does not matter
74             $re = query2re("foo", "-bar"); # => qr/\A(?=.*foo)(?!.*bar).*\z/s -> string must contain 'foo' but must not contain 'bar'
75             $re = query2re({bool=>"or"}, "foo", "bar"); # => qr/\A(?:(?=.*foo)|(?!.*bar)).*\z/s -> string must contain 'foo' or 'bar'
76             $re = query2re({word=>1}, "foo", "bar"); # => qr/\A(?=.*\bfoo\b)(?!.*\bbar\b).*\z/s -> string must contain words 'foo' and 'bar'; 'food' or 'lumbar' won't match
77              
78             =head1 DESCRIPTION
79              
80             This module provides L</query2re> function to convert one or more string queries
81             to a regular expression. Features of the queries:
82              
83             =over
84              
85             =item * Negative searching using the I<-FOO> syntax
86              
87             =back
88              
89             =head1 FUNCTIONS
90              
91             =head2 query2re
92              
93             Usage:
94              
95             my $re = query2re([ \%opts , ] @query);
96              
97             Create a regular expression object from query C<@query>.
98              
99             Known options:
100              
101             =over
102              
103             =item * bool
104              
105             Str. Default C<and>. Either C<and> or C<or>.
106              
107             =item * word
108              
109             Bool. Default false. If set to true, queries must be separate words.
110              
111             =item * ci
112              
113             Bool. Default false. If set to true, will do case-insensitive matching
114              
115             =item * re
116              
117             Bool. Default false. If set to true, will allow regexes in C<@query> as well as
118             converting string queries of the form C</foo/> to regex using
119             L<Regexp::From::String>.
120              
121             =back
122              
123             =head1 HOMEPAGE
124              
125             Please visit the project's homepage at L<https://metacpan.org/release/String-Query-To-Regexp>.
126              
127             =head1 SOURCE
128              
129             Source repository is at L<https://github.com/perlancar/perl-String-Query-To-Regexp>.
130              
131             =head1 SEE ALSO
132              
133             =head1 AUTHOR
134              
135             perlancar <perlancar@cpan.org>
136              
137             =head1 CONTRIBUTING
138              
139              
140             To contribute, you can send patches by email/via RT, or send pull requests on
141             GitHub.
142              
143             Most of the time, you don't need to build the distribution yourself. You can
144             simply modify the code, then test via:
145              
146             % prove -l
147              
148             If you want to build the distribution (e.g. to try to install it locally on your
149             system), you can install L<Dist::Zilla>,
150             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
151             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
152             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
153             that are considered a bug and can be reported to me.
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             This software is copyright (c) 2022 by perlancar <perlancar@cpan.org>.
158              
159             This is free software; you can redistribute it and/or modify it under
160             the same terms as the Perl 5 programming language system itself.
161              
162             =head1 BUGS
163              
164             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=String-Query-To-Regexp>
165              
166             When submitting a bug or request, please include a test-file or a
167             patch to an existing test-file that illustrates the bug or desired
168             feature.
169              
170             =cut