sub unchomp { $_ = $_.$/ foreach @_ ? @_ : $_; } while (<$sock>) { chomp; # Don't want to transform the newline. s/([^[:print:]])/sprintf("\\x{%02X}", ord($1))/eg; unchomp; print; }