#!/usr/bin/perl -lw
#
# Characters, Words and Lines count tool
#

sub checkarg {
    if ( $#ARGV+1 > 1 ) {
	print "ERROR: Too many arguments\n";
	&usage;
    }elsif( $#ARGV+1 < 1 ) {
	print "ERROR: You need more arguments\n";
	&usage;
    }
}

sub usage {
    print "usage: ./wcount.pl input_file\n";
}

&checkarg;

use strict;

my $filename = $ARGV[0];
# my $filename = "test.txt";
my ($charcount,$wordcount,$linecount) = (0,0,0);

open(F,$filename)
    or die "Error: opening $filename: $!";

use locale;
while(<F>){
    $charcount += () = /./gs; #newline counts as a character
    $wordcount += () = /\w+/g;
    $linecount++;
}
no locale;

close F;

print "Characters:  $charcount";
print "     Words:  $wordcount";
print "     Lines:  $linecount";
