in reply to I want to know exactly what comes through a socket

They're unprintable, so you can't see them by definition. You could replace them with some form of representation. For example,

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

Update: Tested. Fixed bugs.

Update: Here's an alternative representation (for charsets based on ASCII):

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

The first program outputs
test\x{1}\x{2}\x{1D}test
and the second outputs
test^A^B^]test
for the result of the Perl expression
"test".chr(1).chr(2).chr(29)."test"

Replies are listed 'Best First'.
Re^2: I want to know exactly what comes through a socket
by GrandFather (Saint) on Nov 23, 2005 at 21:51 UTC
    ...sprintf("\\x{%02X}"...

    may work better and look better. Note the \\!


    DWIM is Perl's answer to Gödel
Re^2: I want to know exactly what comes through a socket
by ivanatora (Sexton) on Nov 23, 2005 at 21:45 UTC
    Can I see their hex values? Or something like that ;]
    I see you use sprintf and ord, but that form of using is too complicated for my poor human mind ;) Could you enlighten me a bit, please?

      The top snippet does display their hex values (embeded in a Perl-style hex character escape).

      ord($1) returns the character number of the character in $1. For a tab, it would return the number '9'.

      sprintf("%02X", ord($1)) returns the hex character number of the character in $1. For a tab, it would return the string '09'.

      sprintf("\\x{%02X}", ord($1)) returns the hex character escape for the character in $1. For a tab, it would return the string '\x{09}'.