Got one more question for you, I have been trying to replace your print "$mac found in all $nfiles input files to print out to a file instead of the screen. I can't seem to get it, I always get some weird type of output: here is what I did.
open(OUT,">$directory") || die couldn't open file:$directory;
print OUT <$mac;
close (OUT);