in reply to read whole file in a directory

Just a couple additional points that you might find useful:

Keep your stop words in a separate text file, and read from that file to build up the regex as kennethk suggested above (this way, the stop-word list can be maintained separately from the program code):

open( STOPWORDS, '<', 'stopword.list' ); my @stopwords = <STOPWORDS>; chomp @stopwords; my $stopregex = join '|', map qr/\b\Q$_\E\b/, @stopwords;
You have a lot of substitutions being done on a word-by-word basis (after splitting each line on whitespace); while the "optimization" value is probably not significant (unless you get into a very large collection of input text), the coding would be much simpler using tr/// on the lines, then splitting to get the tokens:
my %freq; while (<FILE>) { s/<.+?>/ /g; # replace tags with spaces tr/A-Z0-9?!.,:;()*"`'-/a-z /s; # convert upper- to lower-case, an +d # also convert digits, punct to space # NOTE: check your output to see whether any other punctuation or # non-word characters are getting through, and add those to the tr/// # as needed; also: hyphens might need to be treated differently from # other punctuation (keep as-is, or delete, instead of converting to s +pace?) s/$stopregex//g; # remove any/all stop words # at this point, line should contain only word tokens, but # use grep, just in case: for my $token ( grep /[a-z]/, split ) { # only count tokens with + letters $freq{$token}++; } }
Last thing: I don't know if you intended it, but one of the quote characters in the OP code (that is, one of the marks being removed by s///) was apparently a non-ASCII character (U+201D, "right double quotation mark"). If you really are putting utf8 characters in your code, you may need to include use utf8; If your data is utf8 text, you may need to set utf8 mode in the open statement: open( FILE, '<:utf8', $filename )