I think the code below looks cleaner. Depending on what your code does, you can use the memoize module to keep a cache of files you have already checked. Passing $dir_path and $filepath to the subroutine is definitely a good idea. My point here is that it is best to keep subroutines self-contained and not use external variables:
use strict;
use Memoize;
memoize ('read_files');
sub process_files
{
my $dir_path = shift;
if (opendir(TEST, $dir_path))
{
my @files = sort grep{$_ ne '.' and $_ ne '..'} readdir(TEST);
#print "\n@files[0]\n";
read_files("$dir_path\\$_") foreach (@files);
}
else
{
die ("Could not Opendir $dir_path: $!\n");
}closedir TEST;
}
sub read_files
{
my $file_path = shift;
if (-f $file_path)
{
print (DATA "$file_path\n") if ($file_path !~ /(\.lfa|\.zip|\.
+txt|UASTG)$/);
}
else
{
die ("Could not open[$file_path], $!\n");
}
}
The only global here is the DATA filehandle, but you should pass that along through the subroutine chain as a parameter.
-imran