You can take the "embedded" approach if it solves your problem. No need to over-complexify your design by making things orthogonal, assured. But you have to make a promise to yourself: that the very one day that you're going to traverse the data structure to do other things - what about searching an item? - you will not cut-and-paste the code, changing only the line with the print to do something else.

This is called refactoring. It's when you understand that you already solved that problem in one point of your code, and you do a favour to yourself and don't cut-and-paste, but you rather "summarize" what the two functions have in common into a common function that you call twice, in two different context and for two similar-but-different purposes. Why is this a favour to yourself? Because the very time you'll find a bug, you will not have to go hunting in your code to look for all places where you pasted the bug. Because you learn to give these factored-out functions meaningful names, that make your code easier to read and maintain. Because you'll be able to use the same function the next time you have a similar problem.

Sometimes this factoring excercise is done ahead, like in BrowserUK case. This normally happens when you "see" that an algorithm you're going to use would be really helpful in much other situations, and you prepare to reuse. But be lazy, don't look too much ahead, or you could possibly drawn in a sea of pain, where you spend most of the time in building up a beautiful library, and take 5x the needed time to solve your problem.

To be fair, this is what I've summarised from some answers to a previous question of mines: Writing general code: real world example - and doubts! (just to warn you, the first answers actually deal with the issue, then you can find interesting suggestions about tainting, but I understand that these would be a little OT here :)

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

In reply to Re: Passing anon sub as param by polettix
in thread Passing anon sub as param by punkish

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.