parametrical substitution in perl



I want to write a subroutine which performs string substitution
parametrically, as follows:

sub doit()
{
my ($in,$before,$after)=@_;
my $out;
$out = $in;
$out =~ s/$before/$after/e;
return $out;
}

In general this works fine, but if I try and use captured buffers, it
doesn't work. For example, I want to do the following:

s/^(...)/$1 /

i.e. insert a blank after the first three chars.

if I do:

&doit("ABCDEF","^(...)",'$1 ');

I get back "$1 DEF"

Does anyone know how to make this work???

.