in reply to filtering file names to be added to an array...maybe.

One answer is to change your readdir line to this:
my @files = grep /\.dat$/, readdir DIR;
Another answer is to use glob'ing:
while (<*.dat>) { my $file = $_; open(...etc.,etc.); ... } }