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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP PCW

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.