blib/lib/Regexp/Shellish.pm

filter: Statementstmt 100% 47 / 47 Branchbran 91.1% 31 / 34 Conditioncond 89.7% 35 / 39 MC/DCmcdc 84.0% 21 / 25 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 92.8% 142 / 153 CC 31 SCAR 31.7
CC
31
Cov
94%
compile_shellish
29.1
shellish_glob
3.3
BEGIN
1.0
CRAP 23.8
↑ Summary
1 package Regexp::Shellish ;
2  
3 #
4 # Copyright 1999, Barrie Slaymaker <barries@slaysys.com>
5 #
6 # You may distribute under the terms of either the GNU General Public
7 # License or the Artistic License, as specified in the README file.
8 #
9  
10 =head1 NAME
11  
12 Regexp::Shellish - Shell-like regular expressions
13  
14 =head1 SYNOPSIS
15  
16 use Regexp::Shellish qw( :all ) ;
17  
18 $re = compile_shellish( 'a/c*d' ) ;
19  
20 ## This next one's like 'a*d' except that it'll
21 ## match 'a/d'.
22 $re = compile_shellish( 'a**d' ) ;
23  
24 ## And here '**' won't match 'a/d', but behaves
25 ## like 'a*d', except for the possibility of high
26 ## cpu time consumption.
27 $re = compile_shellish( 'a**d', { star_star => 0 } ) ;
28  
29 ## The next two result in identical $re1 and $re2.
30 ## The second is a noop so that Regexp references can
31 ## be easily accomodated.
32 $re1 = compile_shellish( 'a{b,c}d' ) ;
33 $re2 = compile_shellish( qr/\A(?:a(?:b|c)d)\Z/ ) ;
34  
35 @matches = shellish_glob( $re, @possibilities ) ;
36  
37  
38 =head1 DESCRIPTION
39  
40 Provides shell-like regular expressions. The wildcards provided
41 are C<?>, C<*> and C<**>, where C<**> is like C<*> but matches C</>. See
42 L</compile_shellish> for details.
43  
44 Case sensitivity and constructs like <**>, C<(a*b)>, and C<{a,b,c}>
45 can be disabled.
46  
47 =over
48  
49 =cut
50  
51 1 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use Exporter ;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 1 ▶use vars qw( $VERSION @ISA @EXPORT_OK %EXPORT_TAGS ) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 $VERSION = '0.93' ;
59  
60 1 @ISA = qw( Exporter ) ;
61  
62 1 @EXPORT_OK = qw(
63 compile_shellish
64 shellish_glob
65 ) ;
66  
67 1 %EXPORT_TAGS = ( 'all' => \@EXPORT_OK ) ;
68  
69 =item compile_shellish
70  
71 Compiles a string containing a 'shellish' regular expression, returning a
72 Regexp reference. Regexp references passed in are passed through
73 unmolested.
74  
75 Here are the transformation rules from shellish expression terms to
76 perl regular expression terms:
77  
78 Shellish Perl RE
79 ======== =======
80 * [^/]*
81 ? .
82 ** .* ## unless { star_star => 0 }
83 ... .* ## unless { dot_dot_dot => 0 }
84  
85 ( ( ## unless { parens => 0 }
86 ) ) ## unless { parens => 0 }
87  
88 {a,b,c} (?:a|b|c) ## unless { braces => 0 }
89  
90 \a a ## These are de-escaped and
91 \* \* ## passed to quotemeta()
92  
93 The wildcards treat newlines as normal characters.
94  
95 Parens group in to $1..$n, since they are passed through unmolested
96 (unless option parens => 0 is passed). This is useless when using
97 glob_shellish(), though.
98  
99 The final parameter can be a hash reference containing options:
100  
101 compile_shellish(
102 '**',
103 {
104 anchors => 0, ## Doesn't put ^ and $ around the
105 ## resulting regexp
106 case_sensitive => 0, ## Make case insensitive
107 dot_dot_dot => 0, ## '...' is now just three '.' chars
108 star_star => 0, ## '**' is now two '*' wildcards
109 parens => 0, ## '(', ')' are now regular chars
110 braces => 0, ## '{', '}' are now regular chars
111 }
112 ) ;
113  
114 No option affects Regexps passed through.
115  
116 =cut
117  
118 62 ▶sub compile_shellish {
Subroutine compile_shellish: called CC=29 SCAR=33.7
Pod documented
119 62 ▶ my $o = @_ && ref $_[-1] eq 'HASH' ? pop : {} ;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] eq 'HASH' ? : 43 19 62
Condition66%2/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
01943
MC/DC50%1/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
120 62 my $re = shift ;
121  
122 62 ▶ return $re if ref $re eq 'Regexp' ;
Branch: 1 branch
BranchTrueFalseTotal
if ref $re eq "Regexp" 29 33 62
123  
124 my $star_star = ( ! exists $o->{star_star} || $o->{star_star} )
125 33 ▶ ? '.*'
Branch: 1 branch
BranchTrueFalseTotal
!exists $o->{'star_star'} || $o->{'star_star'} ? : 32 1 33
Condition100%3/3
not exists $o->{"star_star"} or $o->{"star_star"}
l!l&&r!l&&!r
3111
MC/DC100%2/2
not exists $o->{"star_star"} or $o->{"star_star"}
not exists $o->{"star_star"} $o->{"star_star"}
Truth table100%3/3
not(A) or B
A= not exists $o->{"star_star"}B= $o->{"star_star"}
ABresult
1X1
011
000
126 : '[^/]*[^/]*' ;
127  
128 my $dot_dot_dot = ( ! exists $o->{dot_dot_dot} || $o->{dot_dot_dot} )
129 33 ▶ ? '.*'
Branch: 1 branch
BranchTrueFalseTotal
!exists $o->{'dot_dot_dot'} || $o->{'dot_dot_dot'} ? : 31 2 33
Condition100%3/3
not exists $o->{"dot_dot_dot"} or $o->{"dot_dot_dot"}
l!l&&r!l&&!r
3012
MC/DC100%2/2
not exists $o->{"dot_dot_dot"} or $o->{"dot_dot_dot"}
not exists $o->{"dot_dot_dot"} $o->{"dot_dot_dot"}
Truth table100%3/3
not(A) or B
A= not exists $o->{"dot_dot_dot"}B= $o->{"dot_dot_dot"}
ABresult
1X1
011
000
130 : '\.\.\.' ;
131  
132 my $case = ( ! exists $o->{case_sensitive} || $o->{case_sensitive} )
133 33 ▶ ? ''
Branch: 1 branch
BranchTrueFalseTotal
!exists $o->{'case_sensitive'} || $o->{'case_sensitive'} ? : 31 2 33
Condition100%3/3
not exists $o->{"case_sensitive"} or $o->{"case_sensitive"}
l!l&&r!l&&!r
2922
MC/DC100%2/2
not exists $o->{"case_sensitive"} or $o->{"case_sensitive"}
not exists $o->{"case_sensitive"} $o->{"case_sensitive"}
Truth table100%3/3
not(A) or B
A= not exists $o->{"case_sensitive"}B= $o->{"case_sensitive"}
ABresult
1X1
011
000
134 : 'i' ;
135  
136 33 ▶ my $anchors = ( ! exists $o->{anchors} || $o->{anchors} ) ;
Condition66%2/3
!exists $o->{'anchors'} || $o->{'anchors'}
l!l&&r!l&&!r
3201
MC/DC50%1/2
!exists $o->{'anchors'} || $o->{'anchors'}
!exists $o->{'anchors'} $o->{'anchors'}
Truth table66%2/3
not(A) || B
A= !exists $o->{'anchors'}B= $o->{'anchors'}
ABresult
1X1
011
000
137 33 ▶ my $pass_parens = ( ! exists $o->{parens} || $o->{parens} ) ;
Condition100%3/3
!exists $o->{'parens'} || $o->{'parens'}
l!l&&r!l&&!r
3111
MC/DC100%2/2
!exists $o->{'parens'} || $o->{'parens'}
!exists $o->{'parens'} $o->{'parens'}
Truth table100%3/3
not(A) || B
A= !exists $o->{'parens'}B= $o->{'parens'}
ABresult
1X1
011
000
138 33 ▶ my $pass_braces = ( ! exists $o->{braces} || $o->{braces} ) ;
Condition100%3/3
!exists $o->{'braces'} || $o->{'braces'}
l!l&&r!l&&!r
3012
MC/DC100%2/2
!exists $o->{'braces'} || $o->{'braces'}
!exists $o->{'braces'} $o->{'braces'}
Truth table100%3/3
not(A) || B
A= !exists $o->{'braces'}B= $o->{'braces'}
ABresult
1X1
011
000
139  
140 33 my $brace_depth = 0 ;
141  
142 33 my $orig = $re ;
143  
144 33 $re =~ s@
145 ( \\.
146 | \*\*
147 | \.\.\.
148 | .
149 )
150 @
151 120 ▶ if ( $1 eq '?' ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($1 eq '?') { } 3 117 120
elsif ($1 eq '*') { } 4 113 117
elsif ($1 eq '**') { } 6 107 113
elsif ($1 eq '...') { } 4 103 107
elsif ($pass_braces and $1 eq '{') { } 4 99 103
elsif ($pass_braces and $1 eq '}') { } 4 95 99
elsif ($pass_braces and $brace_depth and $1 eq ',') { } 2 93 95
elsif ($pass_parens and index('()', $1) != -1) { } 4 89 93
152 3 '[^/]' ;
153 }
154 elsif ( $1 eq '*' ) {
155 4 '[^/]*' ;
156 }
157 elsif ( $1 eq '**' ) {
158 6 $star_star ;
159 }
160 elsif ( $1 eq '...' ) {
161 4 $dot_dot_dot;
162 }
163 ▶ elsif ( $pass_braces && $1 eq '{' ) {
Condition100%3/3
$pass_braces and $1 eq "{"
!ll&&!rl&&r
12874
MC/DC100%2/2
$pass_braces and $1 eq "{"
$pass_braces $1 eq "{"
Truth table100%3/3
A and B
A= $pass_bracesB= $1 eq "{"
ABresult
0X0
100
111
164 4 ++$brace_depth ;
165 4 '(?:' ;
166 }
167 ▶ elsif ( $pass_braces && $1 eq '}' ) {
Condition100%3/3
$pass_braces and $1 eq "}"
!ll&&!rl&&r
12834
MC/DC100%2/2
$pass_braces and $1 eq "}"
$pass_braces $1 eq "}"
Truth table100%3/3
A and B
A= $pass_bracesB= $1 eq "}"
ABresult
0X0
100
111
168 4 ▶ croak "Unmatched '}' in '$orig'" unless $brace_depth-- ;
Branch: 1 branch
BranchTrueFalseTotal
unless $brace_depth-- 0 4 4
169 4 ')' ;
170 }
171 ▶ elsif ( $pass_braces && $brace_depth && $1 eq ',' ) {
Condition100%6/6
$pass_braces and $brace_depth and $1 eq ","
!ll&&!rl&&r
8762
$pass_braces and $brace_depth
!ll&&!rl&&r
12758
MC/DC100%3/3
$pass_braces and $brace_depth and $1 eq ","
$pass_braces $brace_depth $1 eq ","
Truth table100%4/4
A and B and C
A= $pass_bracesB= $brace_depthC= $1 eq ","
ABCresult
0XX0
10X0
1100
1111
172 2 '|' ;
173 }
174 ▶ elsif ( $pass_parens && index( '()', $1 ) >= 0 ) {
Condition100%3/3
$pass_parens and index("()", $1) != -1
!ll&&!rl&&r
5844
MC/DC100%2/2
$pass_parens and index("()", $1) != -1
$pass_parens index("()", $1) != -1
Truth table100%3/3
A and B
A= $pass_parensB= index("()", $1) != -1
ABresult
0X0
100
111
175 4 $1 ;
176 }
177 else {
178 89 quotemeta(substr( $1, -1 ) );
179 }
180 @gexs ;
181  
182 33 ▶ croak "Unmatched '{' in '$orig'" if $brace_depth ;
Branch: 1 branch
BranchTrueFalseTotal
if $brace_depth 0 33 33
183  
184 33 ▶ return $anchors ? qr/\A(?$case:$re)\Z/s : qr/(?$case:$re)/s ;
Branch: 1 branch
BranchTrueFalseTotal
$anchors ? : 32 1 33
185 }
186  
187  
188 =item shellish_glob
189  
190 Pass a regular expression and a list of possible values, get back a list of
191 matching values.
192  
193 my @matches = shellish_glob( '*/*', @possibilities ) ;
194 my @matches = shellish_glob( '*/*', @possibilities, \%options ) ;
195  
196 =cut
197  
198 28 ▶sub shellish_glob {
Subroutine shellish_glob: called CC=3 SCAR=12.0
Pod documented
199 28 ▶ my $o = @_ > 1 && ref $_[-1] eq 'HASH' ? pop : {} ;
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ref $_[-1] eq 'HASH' ? : 0 28 28
Condition33%1/3
@_ > 1 and ref $_[-1] eq "HASH"
!ll&&!rl&&r
0280
MC/DC0%0/2
@_ > 1 and ref $_[-1] eq "HASH"
@_ > 1 ref $_[-1] eq "HASH"
Truth table33%1/3
A and B
A= @_ > 1B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
200 28 my $re = compile_shellish( shift, $o ) ;
201 28 return grep { m/$re/ } @_ ;
202 }
203  
204 =back
205  
206 =head1 AUTHOR
207  
208 Barrie Slaymaker <barries@slaysys.com>
209  
210 =cut
211  
212  
213 1 1 ;
↑ Summary