$str =~ s/(foo)/&do_stuff($str) if length $1 < 50; $1/e;