That's because the | is a special character, and you need to escape it. change $rem ="|"; to $rem ="\\|"; and it'll work. Another (cleaner/easier) option is to use \Q and \E (they escape the characters for you). So change s/$rem/$rep/go; to s/\Q$rem\E/$rep/go;