blib/lib/Filter/Uncomment.pm

filter: Statementstmt 73.4% 36 / 49 Branchbran 40.0% 4 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 9 / 10 Podpod n/a totaltotal 68.9% 51 / 74 CC 7 SCAR 14.2
CC
7
Cov
68%
import
7.7
__ANON__
6.0
BEGIN
1.0
CRAP 4.1
↑ Summary
1  
2 package Filter::Uncomment ;
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 BEGIN
8 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use vars qw ($VERSION @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 $VERSION = '0.03';
12 2 @EXPORT_OK = qw ();
13 2 %EXPORT_TAGS = ();
14 }
15  
16 #-------------------------------------------------------------------------------
17  
18 2 ▶use English qw( -no_match_vars ) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use Readonly ;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 Readonly my $EMPTY_STRING => q{} ;
22  
23 2 ▶use Carp qw(carp croak confess) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Filter::Simple ;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #-------------------------------------------------------------------------------
27  
28 =head1 NAME
29  
30 Filter::Uncomment - Efficiently uncomment sections of your code
31  
32 =head1 SYNOPSIS
33  
34 #~ use Filter::Uncomment qw(multi single) ;
35 #~ use Filter::Uncomment qw(multi) ;
36  
37 use Filter::Uncomment
38 GROUPS =>
39 {
40 multi => ['multi_line', 'multi line with spaces'] ,
41 single => ['single_line', 'single line with spaces'] ,
42 all =>
43 [
44 'multi_line', 'multi line with spaces',
45 'single_line', 'single line with spaces',
46 ] ,
47 };
48  
49 $> perl -MFilter::Uncomment=multi test.pl
50  
51 =head1 DESCRIPTION
52  
53 This module write code that you want be active in only certain circumstances.
54  
55 =head1 DOCUMENTATION
56  
57 Contrast:
58  
59 #example 1
60
61 for my $variable (1 .. $lots)
62 {
63 ## debug Debug($variable) ;
64
65 DoSomethingWith($value) ;
66 }
67  
68 with
69  
70 # example 2
71
72 for my $variable (1 .. $lots)
73 {
74 Debug($variable) if($debug) ;
75
76 DoSomethingWith($value) ;
77 }
78  
79 In example #2, you will always pay for checking the $debug variable. This might be significant in a
80 very tight loop or when you have lots of sections you comment out.
81  
82 B<Filter::Uncomment> is a source code filter that will uncomment sections of perl code only on demand.
83 The uncommenting is done before compile time, you pay only once for it at the program load time.
84  
85 Example #1 would effectively become:
86  
87 #example 1, uncommented
88
89 for my $variable (1 .. $lots)
90 {
91 Debug($variable) ;
92
93 DoSomethingWith($value) ;
94 }
95  
96 B<Filter::Uncomment> can uncomment single line perl comments, or multiline perl comments.
97  
98 ## debug Debug($variable) ;
99
100 =for flag
101
102 PerlCode() ;
103 MorePerlCode() ;
104
105 =cut
106
107 ## tag_can_be_a_single_word HereIsTheCode() ;
108
109 ## or it can be a multiple wors separated by spaces HereIsTheCode() ;
110
111  
112 =head2 Defining tags
113  
114 use Filter::Uncomment
115 GROUPS =>
116 {
117 # name # elements for each group
118 multi => ['multi_line', 'multi line with spaces'] ,
119 single => ['single_line', 'single line with spaces'] ,
120 } ;
121  
122 =head2 Uncommenting
123  
124 Uncommenting is most often done on the command line but can also be done from a module or your script.
125  
126 From the command line:
127  
128 perl -MFilter::Uncomment=multi script.pl
129 perl -MFilter::Uncomment=multi -MFilter::Uncomment=single script.pl
130
131  
132 From a module or script;
133  
134 use Filter::Uncomment qw(multi single) ;
135  
136 =head1 SUBROUTINES/METHODS
137  
138 =cut
139  
140  
141 #-------------------------------------------------------------------------------
142  
143 2 my (%activated, $setup) ;
144  
145 #-------------------------------------------------------------------------------
146  
147 sub import
148 4 ▶{
Subroutine import: called CC=6 SCAR=20.5
149
150 =head2 import
151  
152 This is automatically called for you by Perl
153  
154 =cut
155
156 4 my ($my_name, $argument_type, $argument_value, @other) = @_ ;
157  
158 4 ▶if(defined $argument_type)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $argument_type) { } 2 2 4
159 {
160 2 $setup = undef ;
161  
162 2 ▶ if($argument_type =~ /^GROUPS$/sxm)
Branch: 1 branch
BranchTrueFalseTotal
if ($argument_type =~ /^GROUPS$/msx) { } 2 0 2
163 {
164 2 ▶ unless(defined $argument_value && 'HASH' eq ref $argument_value)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $argument_value and "HASH" eq ref $argument_value) 2 0 2
Condition66%2/3
defined $argument_value and "HASH" eq ref $argument_value
!ll&&!rl&&r
110
MC/DC0%0/2
defined $argument_value and "HASH" eq ref $argument_value
defined $argument_value "HASH" eq ref $argument_value
Truth table66%2/3
A and B
A= defined $argument_valueB= "HASH" eq ref $argument_value
ABresult
0X0
100
111
165 {
166 2 confess "Filter::Uncomment bad 'GROUPS' arguments!\n" ;
167 }
168
169 0 $setup = $argument_value ;
170 }
171 else
172 {
173 0 ▶ my @groups = defined $argument_value
Branch: 1 branch
BranchTrueFalseTotal
defined $argument_value ? : 0 0 0
174 ? ($argument_type, $argument_value, @other)
175 : ($argument_type) ;
176
177 0 @activated{@groups} = (1 .. @groups) ;
178 }
179 }
180 else
181 {
182 2 carp "Filter::Uncomment needs arguments!\n" ;
183 }
184
185 2 return(1) ;
186 }
187  
188 #-------------------------------------------------------------------------------
189  
190 FILTER
191 0 ▶{
Subroutine __ANON__: not called CC=2 SCAR=17.9
192  
193 =head2 FILTER
194  
195 This is automatically called for you by Perl
196  
197 =cut
198  
199 0 ▶if($setup)
Branch: 1 branch
BranchTrueFalseTotal
if ($setup) 0 0 0
200 {
201 my $coumpound_regex
202 = join q[|],
203 0 map {s{\ }{\\\ }sxgm ; $_ ;} # so we can use x option for regex
204 0 map{@{$setup->{$_}}} # elements in the activated groups
205 0 grep {exists $activated{$_}} # only activated groups
206 0 keys %{$setup} ; # all the groups
207
208 #~ print "=> $coumpound_regex\n" ;
209
210 0 s{
211 ^=for\s+ # a pod =for tag
212 (?:$coumpound_regex)\s+ # tag and at least a space
213 (.*?) # pod section content
214 =cut # end of pod section
215 }
216 # section position in your code is kept, line number in errors will be right
217 {
218 $1 # keep only pod section content
219 }xgsm ;
220
221 0 s{
222 \#\# # two octopods
223 (?:$coumpound_regex)\s+ # tag and at least a space
224 }
225 {
226 # replace with nothing
227 }xgsm ;
228
229 }
230 2 } ;
231  
232 #-------------------------------------------------------------------------------
233  
234 2 1 ;
235  
236 =head1 BUGS AND LIMITATIONS
237  
238 None so far.
239  
240 =head1 AUTHOR
241  
242 Khemir Nadim ibn Hamouda
243 CPAN ID: NKH
244 mailto:nadim@khemir.net
245  
246 =head1 LICENSE AND COPYRIGHT
247  
248 This program is free software; you can redistribute
249 it and/or modify it under the same terms as Perl itself.
250  
251 =head1 SUPPORT
252  
253 You can find documentation for this module with the perldoc command.
254  
255 perldoc Filter::Uncomment
256  
257 You can also look for information at:
258  
259 =over 4
260  
261 =item * AnnoCPAN: Annotated CPAN documentation
262  
263 L<http://annocpan.org/dist/Filter-Uncomment>
264  
265 =item * RT: CPAN's request tracker
266  
267 Please report any bugs or feature requests to L <bug-filter-uncomment@rt.cpan.org>.
268  
269 We will be notified, and then you'll automatically be notified of progress on
270 your bug as we make changes.
271  
272 =item * Search CPAN
273  
274 L<http://search.cpan.org/dist/Filter-Uncomment>
275  
276 =back
277  
278 =head1 SEE ALSO
279  
280 The excellent L<Filter::Simple>.
281  
282 =cut
↑ Summary