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   32478 use 5.010001;
  71         217  
2 71     71   327 use strict;
  71         112  
  71         1287  
3 71     71   280 use warnings;
  71         116  
  71         2532  
4              
5             package BSON::Regex;
6             # ABSTRACT: BSON type wrapper for regular expressions
7              
8 71     71   355 use version;
  71         130  
  71         280  
9             our $VERSION = 'v1.12.2';
10              
11 71     71   4835 use Carp ();
  71         166  
  71         1305  
12 71     71   350 use Tie::IxHash;
  71         162  
  71         2020  
13              
14 71     71   370 use Moo;
  71         119  
  71         339  
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   22846 use namespace::clean -except => 'meta';
  71         134  
  71         430  
37              
38             my %ALLOWED_FLAGS = map { $_ => 1 } qw/i m x l s u/;
39              
40             sub BUILD {
41 18625     18625 0 621995 my $self = shift;
42              
43 18625 100       39769 $self->{pattern} = '' unless defined($self->{pattern});
44 18625 100       32052 $self->{flags} = '' unless defined($self->{flags});
45              
46 18625 100       40871 if ( length $self->{flags} ) {
47 18594         23020 my %seen;
48 18594         46862 my @flags = grep { !$seen{$_}++ } split '', $self->{flags};
  18646         76333  
49 18594         35377 foreach my $f (@flags) {
50             Carp::croak("Regex flag $f is not supported")
51 18646 100       38153 if not exists $ALLOWED_FLAGS{$f};
52             }
53              
54             # sort flags
55 18593         117299 $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 1892 my ($self) = @_;
76 3         5 my ( $p, $f ) = @{$self}{qw/pattern flags/};
  3         6  
77 3 100       8 my $re = length($f) ? eval { qr/(?$f:$p)/ } : eval { qr/$p/ };
  1         62  
  2         46  
78 3 50       10 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 349 if ( $ENV{BSON_EXTJSON} ) {
98 23         33 my %data;
99 23         75 tie( %data, 'Tie::IxHash' );
100 23         422 $data{pattern} = $_[0]->{pattern};
101 23         305 $data{options} = $_[0]->{flags};
102             return {
103 23         285 '$regularExpression' => \%data,
104             };
105             }
106              
107 1         204 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.2
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) 2020 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__