File Coverage

blib/lib/BSON/Regex.pm
Criterion Covered Total %
statement 48 48 100.0
branch 13 14 92.8
condition n/a
subroutine 11 11 100.0
pod 2 3 66.6
total 74 76 97.3


line stmt bran cond sub pod time code
1 71     71   27980 use 5.010001;
  71         235  
2 71     71   360 use strict;
  71         122  
  71         1344  
3 71     71   308 use warnings;
  71         129  
  71         2252  
4              
5             package BSON::Regex;
6             # ABSTRACT: BSON type wrapper for regular expressions
7              
8 71     71   420 use version;
  71         186  
  71         310  
9             our $VERSION = 'v1.12.1';
10              
11 71     71   5696 use Carp ();
  71         156  
  71         1246  
12 71     71   344 use Tie::IxHash;
  71         158  
  71         2061  
13              
14 71     71   403 use Moo;
  71         176  
  71         411  
15              
16             #pod =attr pattern
17             #pod
18             #pod A B containing a PCRE regular expression pattern (not a C object
19             #pod and without slashes). Default is the empty string.
20             #pod
21             #pod =cut
22              
23             #pod =attr flags
24             #pod
25             #pod A string with regular expression flags. Flags will be sorted and
26             #pod duplicates will be removed during object construction. Supported flags
27             #pod include C. Invalid flags will cause an exception.
28             #pod Default is the empty string.
29             #pod
30             #pod =cut
31              
32             has [qw/pattern flags/] => (
33             is => 'ro'
34             );
35              
36 71     71   23185 use namespace::clean -except => 'meta';
  71         172  
  71         446  
37              
38             my %ALLOWED_FLAGS = map { $_ => 1 } qw/i m x l s u/;
39              
40             sub BUILD {
41 17997     17997 0 644562 my $self = shift;
42              
43 17997 100       43547 $self->{pattern} = '' unless defined($self->{pattern});
44 17997 100       35518 $self->{flags} = '' unless defined($self->{flags});
45              
46 17997 100       41495 if ( length $self->{flags} ) {
47 17966         24352 my %seen;
48 17966         49027 my @flags = grep { !$seen{$_}++ } split '', $self->{flags};
  18014         71085  
49 17966         37355 foreach my $f (@flags) {
50             Carp::croak("Regex flag $f is not supported")
51 18014 100       40101 if not exists $ALLOWED_FLAGS{$f};
52             }
53              
54             # sort flags
55 17965         117904 $self->{flags} = join '', sort @flags;
56             }
57              
58             }
59              
60             #pod =method try_compile
61             #pod
62             #pod my $qr = $regexp->try_compile;
63             #pod
64             #pod Tries to compile the C and C into a reference to a regular
65             #pod expression. If the pattern or flags can't be compiled, a
66             #pod exception will be thrown.
67             #pod
68             #pod B: Executing a regular expression can evaluate arbitrary
69             #pod code if the L 'eval' pragma is in force. You are strongly advised
70             #pod to read L and never to use untrusted input with C.
71             #pod
72             #pod =cut
73              
74             sub try_compile {
75 3     3 1 1901 my ($self) = @_;
76 3         7 my ( $p, $f ) = @{$self}{qw/pattern flags/};
  3         8  
77 3 100       10 my $re = length($f) ? eval { qr/(?$f:$p)/ } : eval { qr/$p/ };
  1         76  
  2         46  
78 3 50       12 Carp::croak("error compiling regex 'qr/$p/$f': $@")
79             if $@;
80 3         17 return $re;
81             }
82              
83             #pod =method TO_JSON
84             #pod
85             #pod If the C option is true, returns a hashref compatible with
86             #pod MongoDB's L
87             #pod format, which represents it as a document as follows:
88             #pod
89             #pod {"$regularExpression" : { pattern: "", "options" : ""} }
90             #pod
91             #pod If the C option is false, an error is thrown, as this value
92             #pod can't otherwise be represented in JSON.
93             #pod
94             #pod =cut
95              
96             sub TO_JSON {
97 24 100   24 1 198 if ( $ENV{BSON_EXTJSON} ) {
98 23         33 my %data;
99 23         65 tie( %data, 'Tie::IxHash' );
100 23         317 $data{pattern} = $_[0]->{pattern};
101 23         300 $data{options} = $_[0]->{flags};
102             return {
103 23         278 '$regularExpression' => \%data,
104             };
105             }
106              
107 1         202 Carp::croak( "The value '$_[0]' is illegal in JSON" );
108             }
109              
110              
111             1;
112              
113             =pod
114              
115             =encoding UTF-8
116              
117             =head1 NAME
118              
119             BSON::Regex - BSON type wrapper for regular expressions
120              
121             =head1 VERSION
122              
123             version v1.12.1
124              
125             =head1 SYNOPSIS
126              
127             use BSON::Types ':all';
128              
129             $regex = bson_regex( $pattern );
130             $regex = bson_regex( $pattern, $flags );
131              
132             =head1 DESCRIPTION
133              
134             This module provides a BSON type wrapper for a PCRE regular expression and
135             optional flags.
136              
137             =head1 ATTRIBUTES
138              
139             =head2 pattern
140              
141             A B containing a PCRE regular expression pattern (not a C object
142             and without slashes). Default is the empty string.
143              
144             =head2 flags
145              
146             A string with regular expression flags. Flags will be sorted and
147             duplicates will be removed during object construction. Supported flags
148             include C. Invalid flags will cause an exception.
149             Default is the empty string.
150              
151             =head1 METHODS
152              
153             =head2 try_compile
154              
155             my $qr = $regexp->try_compile;
156              
157             Tries to compile the C and C into a reference to a regular
158             expression. If the pattern or flags can't be compiled, a
159             exception will be thrown.
160              
161             B: Executing a regular expression can evaluate arbitrary
162             code if the L 'eval' pragma is in force. You are strongly advised
163             to read L and never to use untrusted input with C.
164              
165             =head2 TO_JSON
166              
167             If the C option is true, returns a hashref compatible with
168             MongoDB's L
169             format, which represents it as a document as follows:
170              
171             {"$regularExpression" : { pattern: "", "options" : ""} }
172              
173             If the C option is false, an error is thrown, as this value
174             can't otherwise be represented in JSON.
175              
176             =for Pod::Coverage BUILD
177              
178             =head1 AUTHORS
179              
180             =over 4
181              
182             =item *
183              
184             David Golden
185              
186             =item *
187              
188             Stefan G.
189              
190             =back
191              
192             =head1 COPYRIGHT AND LICENSE
193              
194             This software is Copyright (c) 2019 by Stefan G. and MongoDB, Inc.
195              
196             This is free software, licensed under:
197              
198             The Apache License, Version 2.0, January 2004
199              
200             =cut
201              
202             __END__