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);