in reply to How can I modify received socket messages in Perl?

First, I doubt your $buffer contains a message, except by luck. But if it does, why not just do

$buffer = "\x{12}\x{34}\x{56}" . $buffer;
or equivalent
$buffer = (map chr, 0x12, 0x34, 0x56) . $buffer;

BTW, are messages in $buffer binary?

Assuming $buffer is being populated by sysread, $buffer is a string where each character represents a byte.

Replies are listed 'Best First'.
Re^2: How can I modify received socket messages in Perl?
by bonny95 (Initiate) on Mar 10, 2009 at 00:57 UTC
    I am sorry that I didn't tell things clearly. I just want to insert 3 bytes into received msg between the header and body, but not in the front or end.

    Can substr() work?
      You don't have a header and body, just a bunch of bytes. First things first, you need to identify the header and the body.
        Yes,you're right. I know I have to identify the header and body, that's why I convert messages in $buffer(binary) into @msg.
        The question is after I insert 3 bytes into @msg, I don't know how to convert modified message in @msg into the type that can be sent out by syswrite().