simple/charhist/charhist.pl

47 lines
844 B
Perl
Raw Permalink Normal View History

#!/usr/bin/perl
use warnings;
use strict;
2017-02-17 13:19:17 +01:00
use autodie;
use Getopt::Long;
2017-02-17 13:19:17 +01:00
use I18N::Langinfo qw(langinfo CODESET);
use Pod::Usage;
2017-02-17 13:19:17 +01:00
my $encoding = langinfo(CODESET);
GetOptions('encoding=s', \$encoding) or pod2usage();
my %hist;
if (@ARGV) {
readfile($_) for @ARGV;
} else {
readfile();
}
2018-09-13 19:02:40 +02:00
my $total = 0;
$total += $_ for values %hist;
binmode STDOUT, ":encoding(UTF-8)";
for (sort keys %hist) {
my $cp = ord;
printf("%x\t%d\t%o\t%s\t%7d\t%f\n",
2018-09-13 19:02:40 +02:00
$cp, $cp, $cp, /\p{Graph}/ ? $_ : ".", $hist{$_}, $hist{$_} / $total);
}
sub readfile {
my ($filename) = @_;
my $fh;
if (defined $filename) {
open $fh, "<", $filename;
} else {
$fh = \*STDIN;
}
binmode $fh, ":encoding($encoding)";
while (<$fh>) {
for my $c (split(//)) {
$hist{$c}++;
}
}
}