blib/lib/SourceCode/LineCounter/Perl.pm

filter: Statementstmt 100% 89 / 89 Branchbran 95.0% 38 / 40 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 31 / 31 Podpod 100% 13 / 13 totaltotal 97.7% 174 / 178 CC 24 SCAR 12.3
CC
24
Cov
98%
count
10.0
_init
3.0
_start_pod
3.0
CRAP 3.4
↑ Summary
1 8 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package SourceCode::LineCounter::Perl;
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 our $VERSION = '1.024';
12  
13 =encoding utf8
14  
15 =head1 NAME
16  
17 SourceCode::LineCounter::Perl - Count lines in Perl source code
18  
19 =head1 SYNOPSIS
20  
21 use SourceCode::LineCounter::Perl;
22  
23 my $counter = SourceCode::LineCounter::Perl->new;
24  
25 $counter->count( $file );
26  
27 my $total_lines = $counter->total;
28  
29 my $pod_lines = $counter->documentation;
30  
31 my $code_lines = $counter->code;
32  
33 my $comment_lines = $counter->comment;
34  
35 my $comment_lines = $counter->blank;
36  
37  
38 =head1 DESCRIPTION
39  
40 This module counts the lines in Perl source code and tries to classify
41 them as code lines, documentation lines, and blank lines.
42  
43 Read a line
44  
45 If it's a blank line, record it and move on to the next line
46  
47 If it is the start of pod, mark that we are in pod, and count
48 it as a pod line and move on
49  
50 If we are in pod and the line is blank, record it as a blank line
51 and a pod line, and move on.
52  
53 If we are ending pod (with C<=cut>, record it as a pod line and
54 move on.
55  
56 If we are in pod and it is not blank, record it as a pod line and
57 move on.
58  
59 If we are not in pod, guess if the line has a comment. If the
60 line has a comment, record it.
61  
62 Removing comments, see if there is anything left. If there is,
63 record it as a code line.
64  
65 Move on to the next line.
66  
67 =cut
68  
69 =over 4
70  
71 =item new
72  
73 =cut
74  
75 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
76 6 my( $class, %hash ) = @_;
77  
78 6 my $self = bless {}, $class;
79 6 $self->_init;
80  
81 6 $self;
82 }
83  
84 =item reset
85  
86 Reset everything the object counted so you can use the same object
87 with another file.
88  
89 =cut
90  
91 1 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
92 1 $_[0]->_init;
93 }
94  
95 =item accumulate( [ BOOLEAN ] )
96  
97 With no argument, returns the current setting as true or false.
98  
99 With one argument, sets the value for accumulation. If that's true,
100 the counter will add to the count from previous calls to C<counter>.
101 If false, C<counter> starts fresh each time.
102  
103 =cut
104  
105 1 ▶sub accumulate {
Subroutine accumulate: called CC=2 SCAR=7.1
Pod documented
106 1 my( $self ) = @_;
107  
108 1 ▶ $self->{accumulate} = !! $_[1] if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 1 1
109  
110 1 return $self->{accumulate};
111 }
112  
113 =item count( FILE )
114  
115 Counts the lines in FILE. The optional second argument, if true,
116 adds those counts to the counts from the last run. By default,
117 previous results are cleared.
118  
119 =cut
120  
121 2 ▶sub count {
Subroutine count: called CC=10 SCAR=23.0
Pod documented
122 2 my( $self, $file ) = @_;
123  
124 2 my $fh;
125 2 ▶ unless( open $fh, "<", $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $file) 1 1 2
126 1 carp "Could not open file [$file]: $!";
127 1 return;
128 }
129  
130 1 ▶ $self->_clear_line_info unless $self->accumulate;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->accumulate 1 0 1
131  
132 1 ▶ LINE: while( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 15 1 16
133 15 chomp;
134 15 $self->_set_current_line( \$_ );
135  
136 15 $self->_total( \$_ );
137 15 ▶ $self->add_to_blank if $self->_is_blank( \$_ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_blank(\$_) 6 9 15
138  
139 15 foreach my $type ( qw( _start_pod _end_pod _pod_line ) ) {
140 42 ▶ $self->$type( \$_ ) && $self->add_to_documentation && next LINE;
Branch: 1 branch
BranchTrueFalseTotal
$self->$type(\$_) and $self->add_to_documentation and next LINE 10 32 42
Condition66%2/3
$self->$type(\$_) and $self->add_to_documentation
!ll&&!rl&&r
32010
MC/DC50%1/2
$self->$type(\$_) and $self->add_to_documentation
$self->$type(\$_) $self->add_to_documentation
Truth table66%2/3
A and B
A= $self->$type(\$_)B= $self->add_to_documentation
ABresult
0X0
100
111
141 }
142  
143 5 ▶ $self->add_to_comment if $self->_is_comment( \$_ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_comment(\$_) 2 3 5
144 5 ▶ $self->add_to_code if $self->_is_code( \$_ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_code(\$_) 1 4 5
145 }
146  
147 1 $self;
148 }
149  
150 8 ▶sub _clear_line_info {
Subroutine _clear_line_info: called CC=1 SCAR=0.0
151 8 $_[0]->{line_info} = {};
152 }
153  
154 15 ▶sub _set_current_line {
Subroutine _set_current_line: called CC=1 SCAR=0.0
155 15 $_[0]->{line_info}{current_line} = \ $_[1];
156 }
157  
158 7 ▶sub _init {
Subroutine _init: called CC=3 SCAR=11.0
159 7 my @attrs = qw(total blank documentation code comment accumulate);
160 7 ▶ foreach ( @attrs ) { $_[0]->{$_} = 0 unless defined $_[0]->{$_} }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]{$_} 36 6 42
161 7 $_[0]->_clear_line_info;
162 };
163  
164 =item total
165  
166 Returns the total number of lines in the file
167  
168 =cut
169  
170 4 ▶sub total { $_[0]->{total} }
Subroutine total: called CC=1 SCAR=0.0
Pod documented
171  
172 22 ▶sub _total { ++ $_[0]->{total} }
Subroutine _total: called CC=1 SCAR=0.0
173  
174 =item documentation
175  
176 Returns the total number of Pod lines in the file, including
177 and blank lines in Pod.
178  
179 =cut
180  
181 10 ▶sub documentation { $_[0]->{documentation} }
Subroutine documentation: called CC=1 SCAR=0.0
Pod documented
182  
183 =item add_to_documentation
184  
185 Add to the documentation line counter if the line is documentation.
186  
187 =cut
188  
189 11 ▶sub add_to_documentation {
Subroutine add_to_documentation: called CC=1 SCAR=0.0
Pod documented
190 11 $_[0]->{line_info}{documentation}++;
191 11 $_[0]->{documentation}++;
192  
193 11 1;
194 }
195  
196 20 ▶sub _start_pod {
Subroutine _start_pod: called CC=3 SCAR=11.0
197 20 ▶ return if $_[0]->_in_pod;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->_in_pod 10 10 20
198 10 ▶ return unless ${$_[1]} =~ /^=\w+/;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$_[1];} =~ /^=\w+/ 8 2 10
199  
200 2 $_[0]->_mark_in_pod;
201  
202 2 1;
203 }
204  
205 17 ▶sub _end_pod {
Subroutine _end_pod: called CC=3 SCAR=11.0
206 17 ▶ return unless $_[0]->_in_pod;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->_in_pod 6 11 17
207 11 ▶ return unless ${$_[1]} =~ /^=cut$/;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$_[1];} =~ /^=cut$/ 9 2 11
208  
209 2 $_[0]->_clear_in_pod;
210  
211 2 1;
212 }
213  
214 26 ▶sub _pod_line {
Subroutine _pod_line: called CC=2 SCAR=6.9
215 26 ▶ return unless $_[0]->_in_pod;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->_in_pod 12 14 26
216 }
217  
218 10 ▶sub _mark_in_pod { $_[0]->{line_info}{in_pod}++ }
Subroutine _mark_in_pod: called CC=1 SCAR=0.0
219 117 ▶sub _in_pod { $_[0]->{line_info}{in_pod} }
Subroutine _in_pod: called CC=1 SCAR=0.0
220 9 ▶sub _clear_in_pod { $_[0]->{line_info}{in_pod} = 0 }
Subroutine _clear_in_pod: called CC=1 SCAR=0.0
221  
222  
223 =item code
224  
225 Returns the number of non-blank lines, whether documentation
226 or code.
227  
228 =cut
229  
230 4 ▶sub code { $_[0]->{code} }
Subroutine code: called CC=1 SCAR=0.0
Pod documented
231  
232 =item add_to_code( LINE )
233  
234 Add to the code line counter if the line is a code line.
235  
236 =cut
237  
238 2 ▶sub add_to_code {
Subroutine add_to_code: called CC=1 SCAR=0.0
Pod documented
239 2 $_[0]->{line_info}{code}++;
240 2 ++$_[0]->{code};
241 }
242  
243 12 ▶sub _is_code {
Subroutine _is_code: called CC=3 SCAR=11.0
244 12 my( $self, $line_ref ) = @_;
245 12 ▶ return if grep { $self->$_() } qw(_is_blank _in_pod);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$self->$_;} "_is_blank", "_in_pod" 2 10 12
246  
247 # this will be false for things in strings!
248 10 ( my $copy = $$line_ref ) =~ s/\s*#.*//;
249  
250 10 ▶ return unless length $copy;
Branch: 1 branch
BranchTrueFalseTotal
unless length $copy 5 5 10
251  
252 5 1;
253 }
254  
255 =item comment
256  
257 The number of lines with comments. These are the things
258 that start with #. That might be lines that are all comments
259 or code lines that have comments.
260  
261 =cut
262  
263 5 ▶sub comment { $_[0]->{comment} }
Subroutine comment: called CC=1 SCAR=0.0
Pod documented
264  
265 =item add_to_comment
266  
267 Add to the comment line counter if the line has a comment. A line
268 might be counted as both code and comments.
269  
270 =cut
271  
272 3 ▶sub add_to_comment {
Subroutine add_to_comment: called CC=1 SCAR=0.0
Pod documented
273 3 $_[0]->{line_info}{comment}++;
274 3 ++$_[0]->{comment};
275 }
276  
277 20 ▶sub _is_comment {
Subroutine _is_comment: called CC=3 SCAR=11.0
278 20 ▶ return if $_[0]->_in_pod;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->_in_pod 4 16 20
279 16 ▶ return unless ${$_[1]} =~ m/#/;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$_[1];} =~ /#/ 7 9 16
280 9 1;
281 }
282  
283 =item blank
284  
285 The number of blank lines. By default, these are lines that
286 match the regex qr/^\s*$/. You can change this in C<new()>
287 by specifying the C<line_ending> parameter.
288  
289 =cut
290  
291 4 ▶sub blank { $_[0]->{blank} }
Subroutine blank: called CC=1 SCAR=0.0
Pod documented
292  
293 =item add_to_blank
294  
295 Add to the blank line counter if the line is blank.
296  
297 =cut
298  
299 7 ▶sub add_to_blank {
Subroutine add_to_blank: called CC=1 SCAR=0.0
Pod documented
300 7 $_[0]->{line_info}{blank}++;
301 7 ++$_[0]->{blank};
302 }
303  
304 36 ▶sub _is_blank {
Subroutine _is_blank: called CC=3 SCAR=11.0
305 36 ▶ return unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 12 24 36
306 24 ▶ return unless ${$_[1]} =~ m/^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$_[1];} =~ /^\s*$/ 13 11 24
307 11 1;
308 }
309  
310 =back
311  
312 =head1 TO DO
313  
314 =over 4
315  
316 =item * Generalized LineCounter that can dispatch to language
317 delegates.
318  
319 =back
320  
321 =head1 SEE ALSO
322  
323  
324 =head1 SOURCE AVAILABILITY
325  
326 This source is in Github
327  
328 https://github.com/briandfoy/sourcecode-linecounter-perl
329  
330 =head1 AUTHOR
331  
332 brian d foy, C<< <briandfoy@cpan.org> >>
333  
334 =head1 COPYRIGHT AND LICENSE
335  
336 Copyright © 2008-2025, brian d foy <bdfoy@cpan.org>. All rights reserved.
337  
338 You may redistribute this under the terms of the Artistic License 2.0.
339  
340 =cut
341  
342 8 1;
↑ Summary