use strict; use warnings; open(my $in, "<", "input.txt") or die "Cannot open < input.txt: $!"; open(my $out, ">", "output.txt") or die "Cannot open < output.txt: $!"; my ($words, $chars) = (0, 0); while (<$in>) { $chars += length($_); $words += scalar(split(/\s+/, $_)); } print $out ("Words=$words\nCharacters=$chars\n");