in reply to Help adding STX and ETX to data string in Perl
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.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);
|
|---|