Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Factoring out common code in two methods of a class

by jvector (Friar)
on Aug 05, 2009 at 18:48 UTC ( [id://786196] : note . print w/replies, xml ) Need Help??


in reply to Re: Factoring out common code in two methods of a class
in thread Factoring out common code in two methods of a class

Thanks for that. I had wondered about "use constant..." but had it in my head that I would need a constant for each 'field' and been put off by the anticipated verbosity of it. But I had been blinkered about putting them all into one constant.

I guess I'll have to leave off the last two parts which in this application are optional, and add them to PACK_TEMPLATE programatically after the length field ... hmm, that's OK.

And yes, you're right that there is a performance implication on constructing that format string each time. Not a lot, but every little bit hurts...


The next signature will be better than this one

Replies are listed 'Best First'.
Re^3: Factoring out common code in two methods of a class
by BrowserUk (Patriarch) on Aug 06, 2009 at 00:07 UTC
    I guess I'll have to leave off the last two parts which in this application are optional, and add them to PACK_TEMPLATE programatically after the length field

    Personally, I'd just create 2 or 3 constants and select between them at runtime:

    use constant T1 = '...'; use constant T2 = t1 . '...'; use constant T3 = T1 . '...'; # or use constant T3 = T2 . '...'; ... my $packed = pack ( T1, T2, T3 )[ condition() ], @bits;

    Wher condition() returns an integer 0 .. 2.


    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.
Re^3: Factoring out common code in two methods of a class
by metaperl (Curate) on Aug 05, 2009 at 19:42 UTC
    I guess I'll have to leave off the last two parts which in this application are optional, and add them to PACK_TEMPLATE programatically after the length field ... hmm, that's OK.
    No, create a function with the 2 optional parts as input and the string $pack_template as output.
      No, create a function with the 2 optional parts as input and the string $pack_template as output.

      What!? What possible merit is there is creating a function to do concatenation? Perl has an operator to do that:

      my $packed = pack PACK_TEMPLATE . (condition ? OPTIONAL_ADDITION : '' +), @bits;

      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.
Re^3: Factoring out common code in two methods of a class
by Anonymous Monk on Aug 06, 2009 at 00:47 UTC
    I had wondered about "use constant..." but had it in my head that I would need a constant for each 'field'

    I often do things like this - that is, reject a solution prematurely based on perceived drawbacks. Usually I haven't thought about the problem in enough depth. I find it's very helpful to sit down and actually WRITE DOWN the requirements (or constraints) for a particular problem, then think through them in detail. (For me, the physical act of writing, be it with keyboard or pen, helps me engage in the thinking-through process.)