http://qs321.pair.com?node_id=887070

HyperDevil has asked for the wisdom of the Perl Monks concerning the following question:

I am programming an AIVDM parser in Perl.
It receives data using UDP (recv), the mechanism seems to work fine.
But the issue is that there are some unknown characters in the message which perl puts into the scalar variable which i cannot seem to get rid of.
I have tried to chomp the scalar which removes the text completely, but length() still reports correctly.
A regex which deletes all hex values which are not numbers or letters, with no effect.
Converted the scalar to hex and back into a another scalar, the new scalar has the same issue.

Code:
while ($sock->recv($msg, 1024)) { $length = length($msg); print "MESSAGE: $msg LENGTH: $length\n"; }
Output is:
MESSAGE: !A LENGTH: 2 MESSAGE: IVDM,1,1,,A,?3nGtL0uPi7pD00,2*46 LENGTH: 34
The first line is ok!
Did you see the second line, where does the space come from?
There is also no newline character in the $msg.

Code:
while ($sock->recv($msg, 1024)) { $length = length($msg); print "MESSAGE: $msg LENGTH: $length"; }
Now without the \n i get this:
MESSAGE: !AI LENGTH: 3MESSAGE: ADM,1,1,,A,13mK@M0P000gVvvTC=4:LgwV2L0B +,0*4C LENGTH: 46MESSAGE: !AIVDM,1,1,,A,40 LENGTH: 16MESSAGE: 2M43AudTF;o0fr +sPTBHl700L0h,0*5A
Now the second message i receive make a new line without me specifieng a \n.

I have been trying to remove these weird characters for days without avail, so i need some help from the experts :)
Update:
while ($sock->recv($msg, 1024)) { $msg1 = substr $msg, 0, -1; print "MESSAGE: $msg1 LENGTH: $length"; }
Does also not give any output at all!