in reply to splitting file with mutiple output filenames

What happens if you get the same value in two different matches?

open O, ">$r" or die $!;
You may want to open the file in append mode: open O, '>>', $r or die $!;

--MidLifeXis