in reply to Does Perl support a given number of replacements ?

I think this will work in all cases:
$x = join 'B', split /a/, $x, 4;