in reply to passing number of files in directory to subroutine

As mentioned above, it is entirely possible (and ultimately likely) that, even when two directories contain the same inventory of file names, readdir() on one will return them in a different order relative to readdir() on the other.

So, to keep things simple, save the file names as hash keys instead of array elements; then in the for loop that calls Calc(), step through the keys of one hash, skip the call if that key is missing from the other hash, otherwise call Calc on the two.

my %onedir = map { $_ => undef } grep /^\w/, readdir(DIR1); my %twodir = map { $_ => undef } grep /^\w/, readdir(DIR2); for my $file ( sort keys %onedir ) { next unless ( exists( %twodir{$file} )); Calc( "$dir1/$file", "$dir2/$file", $testfile ); }