BSON/Regex.pm

filter: Statementstmt 100% 53 / 53 Branchbran 92.8% 13 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 66.6% 2 / 3 totaltotal 97.5% 79 / 81 CC 9 SCAR 11.0
CC
9
Cov
99%
BUILD
6.0
try_compile
3.0
TO_JSON
2.0
CRAP 3.0
« BSON/OID.pm ↑ Summary BSON/DBPointer.pm »
1 71 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 71 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package BSON::Regex;
6 # ABSTRACT: BSON type wrapper for regular expressions
7  
8 71 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 71 our $VERSION = 'v1.12.2';
10  
11 71 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 71 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 71 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #pod =attr pattern
17 #pod
18 #pod A B<string> containing a PCRE regular expression pattern (not a C<qr> 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<imxlsu>. Invalid flags will cause an exception.
28 #pod Default is the empty string.
29 #pod
30 #pod =cut
31  
32 71 has [qw/pattern flags/] => (
33 is => 'ro'
34 );
35  
36 71 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 71 my %ALLOWED_FLAGS = map { $_ => 1 } qw/i m x l s u/;
39  
40 17488 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
41 17488 my $self = shift;
42  
43 17488 ▶ $self->{pattern} = '' unless defined($self->{pattern});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pattern"} 5 17483 17488
44 17488 ▶ $self->{flags} = '' unless defined($self->{flags});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"flags"} 9 17479 17488
45  
46 17488 ▶ if ( length $self->{flags} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"flags"}) 17457 31 17488
47 17457 my %seen;
48 17457 my @flags = grep { !$seen{$_}++ } split '', $self->{flags};
49 17457 foreach my $f (@flags) {
50 Carp::croak("Regex flag $f is not supported")
51 17505 ▶ if not exists $ALLOWED_FLAGS{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ALLOWED_FLAGS{$f} 1 17504 17505
52 }
53  
54 # sort flags
55 17456 $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<pattern> and C<flags> 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<SECURITY NOTE>: Executing a regular expression can evaluate arbitrary
69 #pod code if the L<re> 'eval' pragma is in force. You are strongly advised
70 #pod to read L<re> and never to use untrusted input with C<try_compile>.
71 #pod
72 #pod =cut
73  
74 3 ▶sub try_compile {
Subroutine try_compile: called CC=3 SCAR=11.0
Pod documented
75 3 my ($self) = @_;
76 3 my ( $p, $f ) = @{$self}{qw/pattern flags/};
77 3 ▶ my $re = length($f) ? eval { qr/(?$f:$p)/ } : eval { qr/$p/ };
Branch: 1 branch
BranchTrueFalseTotal
length $f ? : 1 2 3
78 3 ▶ Carp::croak("error compiling regex 'qr/$p/$f': $@")
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
79 if $@;
80 3 return $re;
81 }
82  
83 #pod =method TO_JSON
84 #pod
85 #pod If the C<BSON_EXTJSON> option is true, returns a hashref compatible with
86 #pod MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst>
87 #pod format, which represents it as a document as follows:
88 #pod
89 #pod {"$regularExpression" : { pattern: "<pattern>", "options" : "<flags>"} }
90 #pod
91 #pod If the C<BSON_EXTJSON> 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 24 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=2 SCAR=6.9
Pod documented
97 24 ▶ if ( $ENV{BSON_EXTJSON} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"BSON_EXTJSON"}) 23 1 24
98 23 my %data;
99 23 tie( %data, 'Tie::IxHash' );
100 23 $data{pattern} = $_[0]->{pattern};
101 23 $data{options} = $_[0]->{flags};
102 return {
103 23 '$regularExpression' => \%data,
104 };
105 }
106  
107 1 Carp::croak( "The value '$_[0]' is illegal in JSON" );
108 }
109  
110  
111 71 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<string> containing a PCRE regular expression pattern (not a C<qr> 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<imxlsu>. 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<pattern> and C<flags> 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<SECURITY NOTE>: Executing a regular expression can evaluate arbitrary
162 code if the L<re> 'eval' pragma is in force. You are strongly advised
163 to read L<re> and never to use untrusted input with C<try_compile>.
164  
165 =head2 TO_JSON
166  
167 If the C<BSON_EXTJSON> option is true, returns a hashref compatible with
168 MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst>
169 format, which represents it as a document as follows:
170  
171 {"$regularExpression" : { pattern: "<pattern>", "options" : "<flags>"} }
172  
173 If the C<BSON_EXTJSON> 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 <david@mongodb.com>
185  
186 =item *
187  
188 Stefan G. <minimalist@lavabit.com>
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__
« BSON/OID.pm ↑ Summary BSON/DBPointer.pm »