change replace to perl implementation