I have 2 general rules of thumb.

First: If I need to use a bit of code in more than 1 place, I take that time, instead of a cut/paste, to make it into a sub and change the existing bit of code to call the sub and the new bit of code becomes a call to the same sub.

There are times when the sub needs to be made a bit more generalized and I handle this at that time also.

Second: If I want to use that sub in another script I build that sub into a module which I can then import the function.

At the same time, if it makes sense I try to make the sub into an OO type of interface so I don't need to import the functions into my scripts unless it really makes sense to do it that way.

My experience is that if this overhead is done when there is only 1 existing piece of code which requires changing to use the new interface (sub {} or use Module;) then it is quick and easy to have these nice interfaces so that next time I want this functionality I just type use Module; my $thing=Module->new(...);, or use Module qw( my_keen_sub );

This has a really low cost in the short term and a huge win in the medium to long term, especially if I add a bit of pod to the sub and then a bit more when it gets into a module.


In reply to Re: Perl Module Useage by dga
in thread Perl Module Useage by gawatkins

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.