in reply to foreach gotcha!

I'll go out on a limb and put 10XP that it's a scoping issue, not a Perl bug. The following snippet makes me very suspicious:
my $domain_file; FILE: foreach (@domain_files) { $domain_file = $_;
You're declaring a lexical variable outside of the loop. You use implicit aliasing to the magic $_ with your foreach statement. Then, you copy the contents of $_ to $domain_file.

Hmm. What happens when you leave the loop via last? $domain_file is still in scope, so it will still contain whatever it contained before you broke out of the loop.

I'll go double or nothing that the following will work better for you:

FILE: foreach my $domain_file (@domain_files) {