Sure, it involves more lines (12 now instead of ~8 lines), but why you would deliberately want to throw a costly sub call in a loop?

Because that way my code stays maintainable. You already have two blocks of nearly identical code, and if the call to $filter comes up in different places, you duplicated code for each of these occurrences.

If my code is too slow I can still go back and profile and optimize it.

But duplicate code kills maintainability, because if you fix your code in one place you can be sure you forget to fix it in some other cases.

My example was reduced to the bare minimum that looked useful, but more often the actually duplicated code is larger.


In reply to Re^5: what is sub {1;} by moritz
in thread what is sub {1;} by mlgvalt

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.