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   25282 use 5.010001;
  71         217  
2 71     71   327 use strict;
  71         122  
  71         1220  
3 71     71   296 use warnings;
  71         122  
  71         2247  
4              
5             package BSON::Regex;
6             # ABSTRACT: BSON type wrapper for regular expressions
7              
8 71     71   689 use version;
  71         187  
  71         294  
9             our $VERSION = 'v1.12.0';
10              
11 71     71   5211 use Carp ();
  71         146  
  71         1247  
12 71     71   342 use Tie::IxHash;
  71         132  
  71         1881  
13              
14 71     71   369 use Moo;
  71         170  
  71         351  
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   21216 use namespace::clean -except => 'meta';
  71         161  
  71         411  
37              
38             my %ALLOWED_FLAGS = map { $_ => 1 } qw/i m x l s u/;
39              
40             sub BUILD {
41 16957     16957 0 493581 my $self = shift;
42              
43 16957 100       31781 $self->{pattern} = '' unless defined($self->{pattern});
44 16957 100       30225 $self->{flags} = '' unless defined($self->{flags});
45              
46 16957 100       39192 if ( length $self->{flags} ) {
47 16926         19476 my %seen;
48 16926         38791 my @flags = grep { !$seen{$_}++ } split '', $self->{flags};
  16974         56991  
49 16926         29133 foreach my $f (@flags) {
50             Carp::croak("Regex flag $f is not supported")
51 16974 100       32445 if not exists $ALLOWED_FLAGS{$f};
52             }
53              
54             # sort flags
55 16925         93739 $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 1642 my ($self) = @_;
76 3         5 my ( $p, $f ) = @{$self}{qw/pattern flags/};
  3         7  
77 3 100       8 my $re = length($f) ? eval { qr/(?$f:$p)/ } : eval { qr/$p/ };
  1         70  
  2         42  
78 3 50       11 Carp::croak("error compiling regex 'qr/$p/$f': $@")
79             if $@;
80 3         14 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 193 if ( $ENV{BSON_EXTJSON} ) {
98 23         25 my %data;
99 23         64 tie( %data, 'Tie::IxHash' );
100 23         356 $data{pattern} = $_[0]->{pattern};
101 23         299 $data{options} = $_[0]->{flags};
102             return {
103 23         278 '$regularExpression' => \%data,
104             };
105             }
106              
107 1         199 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.0
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__