in reply to read the whole folder files

in the grep, you have a regex, with a rather strange looking pattern.
my @files = grep {/_*_.txt/} readdir DIR;
perhaps:
my @files = grep {/\.txt$/} readdir DIR;
is all that you need? The $ anchors the regex to the end of the string. An underscore "_" would be unusual before a .txt ending. Without escaping the "." like "\.", the dot means any character, which doesn't look like that you want.

When debugging, print @files to make sure that you are getting the files that you want.