That is a very cumbersome way to do things, as well as being a performance penalty rebuilding the template every time you use it.
The basic problem is that you are setting up a (series of) variable(s), when you should be using a constant!
Yes, the template might need to change in the future, but it won't be changing at runtime, so it is a constant. Both sets of lines can be replaced by a single line (and a comment):
use constant PACK_TEMPLATE = 'H4 b7 b1 b1 b1 b6 H2 A8 H4'; =comment 'H4';# 16 bits Msg ID 'b7';# 7 bits Frag Seq 'b'; # 1 bit More Frags flag 'b'; # 1 bit Unit ID Present 'b'; # 1 bit CRC Present 'b6'; # 6 bits Unused 'H2'; # 8 bits Frag Length 'A8'; # if present, unit ID is 8 chars 'H4'; # if present, CRC is a 16 bit word =cut
Now, you just use PACK_TEMPLATE (give it a better name appropriate to your application), whereever you need it. It will be private to the package/class you declare it in, so there are no action at distance concerns.
In reply to Re: Factoring out common code in two methods of a class
by BrowserUk
in thread Factoring out common code in two methods of a class
by jvector
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |