This file is indexed.

/usr/bin/countperl is in libperl-metrics-simple-perl 0.17-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#!/usr/bin/env perl

use strict;
use warnings;
use English qw(-no_match_vars);
use Carp qw();
use Readonly;
use Pod::Usage qw(pod2usage);
use Perl::Metrics::Simple qw(0.13);

Readonly::Scalar my $MAX_PLAINTEXT_LABEL_LENGTH => 25;
Readonly::Scalar my $EMPTY_STRING               => q{};
Readonly::Scalar my $ONE_SPACE                  => q{ };

our $VERSION = '0.17';

my $COMPLEXITY_LEVEL_THRESHOLD = {
    BTW => 10,
    WTF => 20,
    OMG => 30,
};

my $THRESHOLD_TO_CSS_CLASS = {
    0                                  => 'fyi',
    $COMPLEXITY_LEVEL_THRESHOLD->{BTW} => 'btw',
    $COMPLEXITY_LEVEL_THRESHOLD->{WTF} => 'wtf',
    $COMPLEXITY_LEVEL_THRESHOLD->{OMG} => 'omg',
};

my $CSS = {
    body  => ['font-family:sans-serif;'],
    table => [
        'border-collapse:collapse;', 'border-spacing:0px;',
        'margin:10px 0px;'
    ],
    tr       => [ 'text-align:left;',          'vertical-align:top;' ],
    'td, th' => [ 'border:solid 1px #000000;', 'padding:2px;' ],
    th       => ['background-color:#cccccc;'],
    '.fyi'   => ['background-color:#99ff99;'],
    '.btw'   => ['background-color:#ffff99;'],
    '.wtf'   => ['background-color:#ffcc99;'],
    '.omg'   => ['background-color:#ff9999;'],
    '.w300'  => ['width:300px;'],
    '.w200'  => ['width:200px;'],
    '.w100'  => ['width:100px;'],
    '.right' => ['text-align:right;']
};

my $ANALYSIS;
my $HTML_OUTPUT = 0;
my @FILES       = ();

main();

exit;

sub main {
    parse_opts();

    set_analysis();

    if ($HTML_OUTPUT) {
        print make_html() || Carp::croak "Cannot print! $OS_ERROR";
    }
    else {
        print make_plain() || Carp::croak "Cannot print! $OS_ERROR";
    }
    return;
}

sub parse_opts {
    if ( !@ARGV ) {
        pod2usage(
            -msg     => "Missing required argument(s).\n",
            -exitval => 1,
            -verbose => 1,
        );    # exits program
    }

    foreach (@ARGV) {
        if ( $_ eq '--html' ) {
            $HTML_OUTPUT = 1;
            next;
        }

        if ( $_ eq '--help' ) {
            pod2usage( -verbose => 2, -exitval => 1 );  # exits program
        }

        push @FILES, $_;
    }

    return;
}

sub set_analysis {
    $ANALYSIS = Perl::Metrics::Simple->new()->analyze_files(@FILES);
    return $ANALYSIS;
}

sub _sort_by_complexity {
    return $b->{mccabe_complexity} <=> $a->{mccabe_complexity};
}

### PLAIN

sub make_plain {
    my $report = 'Perl files found: ' . $ANALYSIS->file_count() . "\n\n";
    $report .= make_counts_plain();
    $report .= make_subroutine_size_plain();
    $report .= make_code_complexity_plain();
    $report .= make_list_of_subs_plain();
    return $report;
}

sub make_counts_plain {
    my $counts_plain = make_headline_plain('Counts');
    $counts_plain .= make_line_plain( 'total code lines', $ANALYSIS->lines() );
    $counts_plain .= make_line_plain( 'lines of non-sub code',
        $ANALYSIS->main_stats()->{lines} );
    $counts_plain
        .= make_line_plain( 'packages found', $ANALYSIS->package_count() );
    $counts_plain .= make_line_plain( 'subs/methods', $ANALYSIS->sub_count() );
    $counts_plain .= "\n\n";
    return $counts_plain;
}

sub make_line_plain {
    my ( $key, $value ) = @_;
    my $line = $key . q{:};
    $line .= $ONE_SPACE x ( $MAX_PLAINTEXT_LABEL_LENGTH - length $key );
    $line .= $value . "\n";
    return $line;
}

sub make_headline_plain {
    my ($headline) = @_;
    my $formatted_headline = $headline . "\n";
    $formatted_headline .= q{-} x length $headline;
    $formatted_headline .= "\n";
    return $formatted_headline;
}

sub make_subroutine_size_plain {
    my $subroutine_size_plain = make_headline_plain('Subroutine/Method Size');
    $subroutine_size_plain .= make_line_plain( 'min',
        $ANALYSIS->summary_stats()->{sub_length}->{min} );
    $subroutine_size_plain .= make_line_plain( 'max',
        $ANALYSIS->summary_stats()->{sub_length}->{max} );
    $subroutine_size_plain .= make_line_plain( 'mean',
        $ANALYSIS->summary_stats()->{sub_length}->{mean} );
    $subroutine_size_plain .= make_line_plain( 'std. deviation',
        $ANALYSIS->summary_stats()->{sub_length}->{standard_deviation} );
    $subroutine_size_plain .= make_line_plain( 'median',
        $ANALYSIS->summary_stats()->{sub_length}->{median} );

    $subroutine_size_plain .= "\n\n";
    return $subroutine_size_plain;
}

sub make_code_complexity_plain {
    my $code_complexity_plain = make_headline_plain('McCabe Complexity');

    $code_complexity_plain
        .= make_complexity_section_plain( 'Code not in any subroutine',
        'main_complexity' );
    $code_complexity_plain .= "\n";
    $code_complexity_plain
        .= make_complexity_section_plain( 'Subroutines/Methods',
        'sub_complexity' );

    $code_complexity_plain .= "\n\n";
    return $code_complexity_plain;
}

sub make_complexity_section_plain {
    my ( $section, $key ) = @_;

    my $complexity_section_plain = $section . "\n";

    $complexity_section_plain
        .= make_line_plain( 'min', $ANALYSIS->summary_stats()->{$key}->{min} );
    $complexity_section_plain
        .= make_line_plain( 'max', $ANALYSIS->summary_stats()->{$key}->{max} );
    $complexity_section_plain .= make_line_plain( 'mean',
        $ANALYSIS->summary_stats()->{$key}->{mean} );
    $complexity_section_plain .= make_line_plain( 'std. deviation',
        $ANALYSIS->summary_stats()->{$key}->{standard_deviation} );
    $complexity_section_plain .= make_line_plain( 'median',
        $ANALYSIS->summary_stats()->{$key}->{median} );
    return $complexity_section_plain;
}

sub make_list_of_subs_plain {
    my @main_from_each_file
        = map { $_->{main_stats} } @{ $ANALYSIS->file_stats() };
    my @sorted_subs = sort _sort_by_complexity(),
        ( @{ $ANALYSIS->subs() }, @main_from_each_file );

    my $column_widths = get_column_widths(@main_from_each_file);

    my $list_of_subs_plain
        = make_headline_plain('List of subroutines, with most complex at top');

    $list_of_subs_plain
        .= make_column( 'complexity', $column_widths->{mccabe_complexity} );
    $list_of_subs_plain .= make_column( 'sub',  $column_widths->{name} );
    $list_of_subs_plain .= make_column( 'path', $column_widths->{path} );
    $list_of_subs_plain .= make_column( 'size', $column_widths->{lines} );
    $list_of_subs_plain .= "\n";

    foreach my $sub (@sorted_subs) {
        $list_of_subs_plain
            .= make_list_of_subs_line_plain( $sub, $column_widths );
    }

    $list_of_subs_plain .= "\n\n";
    return $list_of_subs_plain;
}

sub make_list_of_subs_line_plain {
    my ( $sub, $column_widths ) = @_;

    my $list_of_subs_line_plain;

    foreach my $col ( 'mccabe_complexity', 'name', 'path', 'lines' ) {
        $list_of_subs_line_plain
            .= make_column( $sub->{$col}, $column_widths->{$col} );
    }

    $list_of_subs_line_plain .= "\n";
    return $list_of_subs_line_plain;
}

sub make_column {
    my ( $value, $width ) = @_;

    my $column = $value;
    $column .= $ONE_SPACE x ( $width - length($value) + 2 );
    return $column;
}

sub get_column_widths {
    my @main_from_each_file = @_;

    my $column_widths = {
        mccabe_complexity => 10,
        name              => 3,
        path              => 4,
        lines             => 4,
    };

    foreach my $sub (@main_from_each_file) {
        foreach my $col ( 'mccabe_complexity', 'name', 'path', 'lines' ) {
            if ( length( $sub->{$col} ) > $column_widths->{$col} ) {
                $column_widths->{$col} = length( $sub->{$col} );
            }
        }
    }

    return $column_widths;
}

### /PLAIN
### HTML

sub make_html {
    my $html = '<!DOCTYPE html><html lang="en">';

    $html .= make_head();

    $html .= make_body();

    $html .= '</html>';
    return $html;
}

sub make_head {
    my $head = '<head><title>countperl</title><meta charset="utf-8">';

    $head .= make_css();

    $head .= '</head>';
    return $head;
}

sub make_css {
    my $css = '<style type="text/css">';

    foreach my $selector ( keys %{$CSS} ) {
        $css .= $selector . '{';

        foreach ( @{ $CSS->{$selector} } ) {
            $css .= $_;
        }

        $css .= '}';
    }

    $css .= '</style>';

    return $css;
}

sub make_body {
    my $body = '<body><h3>';
    $body .= 'Perl files found ' . $ANALYSIS->file_count();
    $body .= '</h3>';

    $body .= make_counts_html();
    $body .= make_subroutine_size_html();
    $body .= make_code_complexity_html();
    $body .= make_list_of_subs_html();
    $body .= make_complexity_levels();

    $body .= '</body>';
    return $body;
}

sub make_counts_html {
    my $counts_html = '<table><tr><th colspan="2">Counts</th></tr>';

    $counts_html .= make_tr( 'total code lines', $ANALYSIS->lines() );
    $counts_html
        .= make_tr( 'lines of non-sub code', $ANALYSIS->main_stats()->{lines} );
    $counts_html .= make_tr( 'packages found', $ANALYSIS->package_count() );
    $counts_html .= make_tr( 'subs/methods',   $ANALYSIS->sub_count() );

    $counts_html .= '</table>';
    return $counts_html;
}

sub make_tr {
    my ( $key, $value, $css ) = @_;
    $css = $css ? $ONE_SPACE . $css : $EMPTY_STRING;

    my $tr = '<tr><td class="w200">';
    $tr .= $key;
    $tr .= '</td><td class="w100 right' . $css . '">';
    $tr .= $value;
    $tr .= '</td></tr>';

    return $tr;
}

sub make_subroutine_size_html {
    my $subroutine_size_html
        = '<table><tr><th colspan="2">Subroutine/Method Size</th></tr>';

    my $min                = $ANALYSIS->summary_stats()->{sub_length}->{min}                || 0;
    my $max                = $ANALYSIS->summary_stats()->{sub_length}->{max}                || 0;
    my $mean               = $ANALYSIS->summary_stats()->{sub_length}->{mean}               || '0.00';
    my $standard_deviation = $ANALYSIS->summary_stats()->{sub_length}->{standard_deviation} || '0.00';
    my $median             = $ANALYSIS->summary_stats()->{sub_length}->{median}             || '0.00';

    $subroutine_size_html .= make_tr( 'min',            $min );
    $subroutine_size_html .= make_tr( 'max',            $max );
    $subroutine_size_html .= make_tr( 'mean',           $mean );
    $subroutine_size_html .= make_tr( 'std. deviation', $standard_deviation );
    $subroutine_size_html .= make_tr( 'median',         $median );

    $subroutine_size_html .= '</table>';

    return $subroutine_size_html;
}

sub make_code_complexity_html {
    my $code_complexity_html
        = '<table><tr><th colspan="3">McCabe Complexity</th></tr>';

    $code_complexity_html
        .= make_complexity_section_html( 'Code not in any subroutine',
        'main_complexity' );
    $code_complexity_html
        .= make_complexity_section_html( 'Subroutines/Methods',
        'sub_complexity' );

    $code_complexity_html .= '</table>';

    return $code_complexity_html;
}

sub make_complexity_section_html {
    my ( $section, $key ) = @_;

    my $complexity_section_html
        = '<tr><td rowspan="5" class="w200">' . $section . '</td>';

    my $min                = $ANALYSIS->summary_stats()->{$key}->{min}                || 0;
    my $max                = $ANALYSIS->summary_stats()->{$key}->{max}                || 0;
    my $mean               = $ANALYSIS->summary_stats()->{$key}->{mean}               || '0.00';
    my $standard_deviation = $ANALYSIS->summary_stats()->{$key}->{standard_deviation} || '0.00';
    my $median             = $ANALYSIS->summary_stats()->{$key}->{median}             || '0.00';

    $complexity_section_html .= '<td class="w200">min</td><td class="w100 right ' . get_class_by_count( $min ) . '">';
    $complexity_section_html .= $min;
    $complexity_section_html .= '</td></tr>';

    $complexity_section_html .= make_tr( 'max',            $max,                get_class_by_count( $max ) );
    $complexity_section_html .= make_tr( 'mean',           $mean,               get_class_by_count( $mean ) );
    $complexity_section_html .= make_tr( 'std. deviation', $standard_deviation, get_class_by_count( $standard_deviation ) );
    $complexity_section_html .= make_tr( 'median',         $median,             get_class_by_count( $median ) );

    return $complexity_section_html;
}

sub make_list_of_subs_tr {
    my ($sub) = @_;

    my $list_of_subs_tr
        = '<tr><td class="'
        . get_class_by_count( $sub->{mccabe_complexity} )
        . ' right">'
        . $sub->{mccabe_complexity}
        . '</td><td>'
        . $sub->{name}
        . '</td><td>'
        . $sub->{path}
        . '</td><td class="right">'
        . $sub->{lines}
        . '</td></tr>';

    return $list_of_subs_tr;
}

sub make_list_of_subs_html {
    my @main_from_each_file
        = map { $_->{main_stats} } @{ $ANALYSIS->file_stats() };
    my @sorted_subs = sort _sort_by_complexity(),
        ( @{ $ANALYSIS->subs() }, @main_from_each_file );

    my $list_of_subs_html
        = '<table><tr><th colspan="4">List of subroutines, with most complex at top</th></tr>'
        . '<tr><td class="w100">complexity</td><td>sub</td><td>path</td><td class="w100">size</td><tr>';

    foreach my $sub (@sorted_subs) {
        $list_of_subs_html .= make_list_of_subs_tr($sub);
    }

    $list_of_subs_html .= '</table>';

    return $list_of_subs_html;
}

sub make_complexity_levels {
    my $complexity_levels
        = '<table><tr><th colspan="2">Complexity Levels</th></tr>';

    foreach my $level ( sort keys %{$THRESHOLD_TO_CSS_CLASS} ) {
        $complexity_levels
            .= '<tr><td class="'
            . $THRESHOLD_TO_CSS_CLASS->{$level} . '">'
            . $THRESHOLD_TO_CSS_CLASS->{$level}
            . '</td><td class="'
            . $THRESHOLD_TO_CSS_CLASS->{$level} . '">'
            . '&gt;= '
            . $level
            . '</td></tr>';
    }

    $complexity_levels .= '</table>';

    return $complexity_levels;
}

sub get_class_by_count {
    my ($count) = @_;

    my @level = reverse sort keys %{$THRESHOLD_TO_CSS_CLASS};

    foreach (@level) {
        return $THRESHOLD_TO_CSS_CLASS->{$_} if ( $count >= $_ );
    }
    return;
}

### /HTML

__END__

=head1 NAME

countperl - count lines, packages, subs and complexity of Perl files.

=head1 USAGE

B<countperl> F<FILE_OR_DIRECTORY> [F<FILE_OR_DIRECTORY> ....] [--html] [--help]

=head1 REQUIRED ARGUMENTS

At least one file or directory path must be supplied.

=head1 OPTIONS

=over 4

=item --help

Prints documentation to STDERR.

=item --html

Produces HTML output instead of the plain-text default.

=back

=head1 CONFIGURATION

N/A. Currently no support for any configuration files.

=head1 EXIT STATUS

Exits zero on success, non-zero on failure.

=head1 DESCRIPTION

F<countperl> uses B<Perl::Metrics::Simple> to examines the named files and
recursivesly searches named directories for Perl files.


Perl files are identified by B<Perl::Metrics::Simple-E<gt>is_perl_file>. Basically
if the file ends in C<.pl>, C<.pm>, or C<.t> or has what appears to be a perl
I<shebang> line.

F<countperl> produces a report on F<STDOUT> of counts of total lines,
packages, subroutines/methods,
the minimum, maximum, mean, standard deviation, and median size and
mccabe_complexity (cyclomatic complexity) of subroutines and
the 'main' portion of each file (everything not in a subroutine.)

=head2 Output Format

Line counts do not include comments nor pod.

The current output format is human-readable text:

    Perl files found:                3

    Counts
    ------
    total code lines:       856
    lines of non-sub code:  450
    packages found:           3
    subs/methods:            42

    Subroutine/Method Size
    ----------------------
    min:                  3 lines
    max:                  32 lines
    mean:                 9.67 lines
    std. deviation:       7.03
    median:               7.50

    McCabe Complexity
    -----------------
    Code not in any subroutine::
    min:                  1
    max                   1
    mean:                 1.00
    std. deviation:       0.00
    median:               1.00

    Subroutines/Methods:
    min:                  1
    max:                  5
    avg:                  1.00
    std. deviation:       1.36
    median:               1.00

    Tab-delimited list of subroutines, with most complex at top
    -----------------------------------------------------------
    complexity      sub     path    size
    5       is_perl_file    lib/Perl/Metrics/Simple.pm      11
    5       _has_perl_shebang       lib/Perl/Metrics/Simple.pm      13
    5       _init   lib/Perl/Metrics/Simple/Analysis/File.pm        30
    4       find_files      lib/Perl/Metrics/Simple.pm      11
    4       new     lib/Perl/Metrics/Simple/Analysis.pm     10
    4       is_ref  lib/Perl/Metrics/Simple/Analysis.pm     8

With --html switch output format is HTML.

=head1 VERSION

This is version 0.031 of F<countperl>.

=head1 DIAGNOSTICS

Prints usage message to STDERR if required arguments are not provided.

=head1 INCOMPATIBILITIES

None known.

=head1 BUGS AND LIMITATIONS

=head2 Bugs
No bugs reported yet :-)
See: http://rt.cpan.org/NoAuth/Bugs.html?Dist=Perl-Metrics-Simple

=head2 Limitations

=over 4

=item Does not accept input from STDIN.

=item No machine-readable report format available (e.g. XML, tab-delimited)

=back

=head1 SUPPORT

Via CPAN:

=head2 Disussion Forum

http://www.cpanforum.com/dist/Perl-Metrics-Simple

=head2 Bug Reports

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Perl-Metrics-Simple

=head1 DEPENDENCIES

=over 4

=item L<Perl::Metrics::Simple|Perl::Metrics::Simple> 0.13 (which depends upon L<PPI|PPI>.)

=item L<Pod::Usage|Pod::Usage>

=back

=head1 SEE ALSO

=over 4

=item L<PPI|PPI>

=item L<Perl::Critic|Perl::Critic>

=item L<Perl::Metrics|Perl::Metrics>

=item http://en.wikipedia.org/wiki/Cyclomatic_complexity

=back

=head1 AUTHOR

    Matisse Enzer
    CPAN ID: MATISSE
    Eigenstate Consulting, LLC
    matisse@eigenstate.net
    http://www.eigenstate.net/

=head1 LICENSE AND COPYRIGHT

This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the
LICENSE file included with this module.

=cut