File: longlines.pl

package info (click to toggle)
php-openid 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 3,284 kB
  • ctags: 6,733
  • sloc: php: 20,482; python: 346; xml: 333; sh: 130; perl: 103; makefile: 31
file content (46 lines) | stat: -rw-r--r-- 1,049 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/env perl -w

use strict;

my $filename = $ARGV[0];

if (!$filename) {
    print "Usage: longlines.pl <filename> [length]\n";
    exit(1);
}

# Set a default maximum line length.
my $max_length = $ARGV[1] || 80;

my @lines = ();
my $line_num = 0;

open(HANDLE, "<", $filename) or die "Cannot open $filename\n";

# Read the file and track the lines with length > $max_length.
while (<HANDLE>) {
    $line_num++;
    # Subtract one because the newline doesn't count toward the
    # length.
    if (length($_) - 1 > $max_length) {
        push @lines, $line_num;
    }
}

# If more than five long lines were found, truncate to five and
# indicate that others were present, too.
if (@lines > 5) {
    @lines = @lines[0..4];
    push @lines, "and others";
}

# If any long lines were found, notify and exit(1); otherwise,
# exit(0).
if (@lines) {
    print $filename." (line".((@lines > 1) ? "s" : "")." ".
        join(", ", @lines)." exceed".((@lines == 1) ? "s" : "").
        " length $max_length)\n";
    exit(1);
} else {
    exit(0);
}