in reply to pattern matching

The answer to your first question can be found in perlop in the section Regexp Quote-Like Operators

If you use the /s modifier, then . matches any character, including newlines. So for example:

"a\nb" =~ m/a.*b/; # no match "a\nb" =~ m/a.*b/s; # matches

For handling TCP packets I'd rather use an existing module from CPAN.