in reply to Help adding STX and ETX to data string in Perl

Both the other replies look like they'll work! I'd advise taking either of them, and turning them into a function that takes in the text to encode and the maximum length, and either returns the correctly-encoded string, or does a die on error. Something like (modifying jwkrahn's version):
use constant MaxMessageLength => 300_000; sub stx_encode { my ($Message, $max_length) = @_; my $MsgLgth = 18 + length $Message; die "Formatted message exceeds MaxMessageLength" if $MsgLgth >= $max +_length; return sprintf "\x2%08d00000000%s\x3", $MsgLgth, $Message; } print stx_encode('hello', MaxMessageLength);
The benefit of making a function is it's more widely reusable, and is generally better practice as one can write (and test) each piece of a program bit by bit. Edit: and creating stx_decode would be an obvious step, and then you'd have two little functions that nicely wrap that format.