forked from openresty/nginx-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc.pl
executable file
·54 lines (42 loc) · 998 Bytes
/
wc.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use encoding 'utf8';
if (!@ARGV) {
die "No input file specified.\n";
}
my $hanc = 0;
my $wc = 0;
my $c = 0;
my $lc = 0;
for my $infile (@ARGV) {
open my $in, "<:encoding(UTF-8)", $infile
or
die "cannot open $infile for reading: $!\n";
while (<$in>) {
$lc++;
$hanc++ while /\p{Han}/g;
while (1) {
if (/\G\s+/gc) {
next;
} elsif (/\G(?:\p{Han}|[”“,;:?。!…]|[,;.])/gc) {
$wc++;
next;
} elsif (/\G[A-Za-z0-9_]+/gc) {
$wc++;
next;
} elsif (/\G./gc) {
next;
}
last;
}
$c += length($_);
}
close $in;
}
my $rough_wc = int($wc * 1.2);
print "$hanc han chars found.\n";
print "$wc words found.\n";
print "$rough_wc rough words found.\n";
print "$lc lines found.\n";
print "$c chars found.\n";