in reply to requires explicit package name

Please, before pasting an entire script, take a few minutes to strip the script down to a small script that demonstrates the failure. 90% of the time, doing this will reveal the bug.

In this case, your problem is here:

... { my $name = $&; } my $outfile = "outfile[$name].txt";
The scope of the first $name ends at the end of the block that it's in. The $name used in the next statement is a different variable. That's clearly not what you intend. The fix is to declare my $name; higher up in the script.