Some suggestions and thoughts:
- Although I can't be certain, it looks like you're not using strict. Always use warnings; use strict;!!
- opendir( DIR, $sd) || die; would better with a lexical handle and the error message: opendir(my $dh, $sd) || die "couldn't opendir $sd: $!";
- Your opens would be better and safer with the three-argument form, lexical filehandles, and error handling: open (my $fh, '<', $dots[$a]) or die "couldn't open $dots[$a]: $!";
- The condition "$sd\/$filename" =~ /\/\./ will not just match (i.e. skip) .dotfiles, it'll skip any filenames that contain /. anywhere. For example, if $sd happens to contain that string someday, all files will be rejected. The condition $filename=~/^\./ would skip only filenames that begin with a dot and so that's probably better.
- I find the name @dots a little strange since it contains only files that don't begin with a dot?
- The last file of @dots is opened and read twice. An alternative would be to read <FILE> into a temporary array and store that into @foo, and into @foo2 when appropriate. Or you could move the special treatment of $dots[-1] outside of the loop (that would also have the stylistic advantage that the loop variable $a could be eliminated, e.g. for my $dot (@dots) { open (my $fh, '<', $dot) ...).
- I think the condition $a+1 eq @dots is more clearly written as $a==$#dots.