in reply to Global symbol .... requires explicit package name at

foreach $files (@files)
I would change this line to:
foreach my $file (@files)
That would distinguish a single file from your array of files. Of course, you must change every instance of $files to $file inside your foreach loop. See also Foreach Loops