1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/t/RegularExpressions/ProhibitUnusedCapture.run $
3 # $Date: 2008-03-16 17:40:45 -0500 (Sun, 16 Mar 2008) $
6 ##############################################################################
19 #-----------------------------------------------------------------------------
21 ## name assignment captures
26 my ($foo) = m/(foo|bar)/;
27 my ($foo) = m/(foo)(?:bar)/;
30 my %foo = m/(foo)(bar)/g;
32 my ($foo, $bar) = m/(foo)(bar)/;
33 my @foo = m/(foo)(bar)/;
34 my ($foo, @bar) = m/(foo)(bar)/;
35 my ($foo, @bar) = m/(foo)(bar)(baz)/;
37 #-----------------------------------------------------------------------------
39 ## name undef array captures
45 my ($foo) =()= m/(foo)/g;
47 #-----------------------------------------------------------------------------
49 ## name complex array assignment captures
53 @$foo = m/(foo)(bar)/;
54 @{$foo} = m/(foo)(bar)/;
55 %$foo = m/(foo)(bar)/;
56 %{$foo} = m/(foo)(bar)/;
58 ($foo,@$foo) = m/(foo)(bar)/;
59 ($foo,@{$foo}) = m/(foo)(bar)/;
61 #-----------------------------------------------------------------------------
63 ## name conditional captures
77 my ($foo, $bar) = ($1, $2);
86 # bug, but not a violation of THIS policy
91 #-----------------------------------------------------------------------------
93 ## name boolean and ternary captures
98 m/(foo)/ ? print $1 : die;
99 m/(foo)/ && ($1 == 'foo') ? print 1 : die;
101 #-----------------------------------------------------------------------------
103 ## name loop captures
112 #-----------------------------------------------------------------------------
114 ## name slurpy array loop captures
118 map {print} m/(foo)/;
120 foo('bar', m/(foo)/);
121 foo(m/(foo)/, 'bar');
126 ## name slurpy with assignment
130 my ($foo) = grep {$b++ == 2} m/(foo)/g;
131 my ($foo) = grep {$b++ == 2} $str =~ m/(foo)/g;
133 #-----------------------------------------------------------------------------
135 ## name slurpy with array assignment
139 my @foo = grep {$b++ > 2} m/(foo)/g;
140 my @foo = grep {$b++ > 2} $str =~ m/(foo)/g;
142 #-----------------------------------------------------------------------------
144 ## name assignment captures on string
148 my ($foo) = $str =~ m/(foo)/;
149 my ($foo) = $str =~ m/(foo|bar)/;
150 my ($foo) = $str =~ m/(foo)(?:bar)/;
151 my @foo = $str =~ m/(foo)/;
152 my @foo = $str =~ m/(foo)/g;
154 my ($foo, $bar) = $str =~ m/(foo)(bar)/;
155 my @foo = $str =~ m/(foo)(bar)/;
156 my ($foo, @bar) = $str =~ m/(foo)(bar)/;
157 my (@bar) = $str =~ m/(foo)(bar)/;
158 my ($foo, @bar) = $str =~ m/(foo)(bar)(baz)/;
160 #-----------------------------------------------------------------------------
162 ## name slurpy captures on string
166 map {print} $str =~ m/(foo)/g;
168 #-----------------------------------------------------------------------------
170 ## name self captures
177 #-----------------------------------------------------------------------------
179 ## name basic failures
181 ## optional_modules Regexp::Parser
185 my ($foo) = m/(foo)/g;
199 #-----------------------------------------------------------------------------
201 ## name negated regexp failures
203 ## optional_modules Regexp::Parser
206 my ($foo) = $str !~ m/(foo)/;
208 #-----------------------------------------------------------------------------
210 ## name statement failures
212 ## optional_modules Regexp::Parser
215 m/(foo)/ && m/(bar)/ && print $1;
217 #-----------------------------------------------------------------------------
221 ## optional_modules Regexp::Parser
230 #-----------------------------------------------------------------------------
232 ## name anon sub failures
234 ## optional_modules Regexp::Parser
235 ## TODO PPI v1.118 doesn't recognize anonymous subroutines
244 #-----------------------------------------------------------------------------
246 ## name ref constructors
250 $f = { m/(\w+)=(\w+)/g };
253 #-----------------------------------------------------------------------------
265 map { m/(foo)/ } (1, 2, 3);
267 #-----------------------------------------------------------------------------
269 ## name failing regexp with syntax error
275 #-----------------------------------------------------------------------------
277 ## name lvalue sub assigment pass
281 (substr $str, 0, 1) = m/(\w+)/;
283 #-----------------------------------------------------------------------------
285 ## name lvalue sub assigment failure
287 ## optional_modules Regexp::Parser
288 ## TODO lvalue subs are too complex to support
291 (substr $str, 0, 1) = m/(\w+)(\d+)/;
293 #-----------------------------------------------------------------------------
295 ## name code coverage
297 ## optional_modules Regexp::Parser
305 #-----------------------------------------------------------------------------
309 # cperl-indent-level: 4
311 # indent-tabs-mode: nil
312 # c-indentation-style: bsd
314 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :