If you do this you will quickly encounter the following problems:
  1. Tracking down where a routine comes from will become difficult.
  2. Scripts will reuse variables and subroutine names unbeknownst to you, and they will collide.
  3. Because of hardcoded paths it will be very hard for you to test any changes without modifying production.
  4. Without a clear division between executable and non-executable code, people are likely to put actions into scripts where they do not belong.
In addition the speed difference between doing the above and doing it right with modules is insignificant if you have ever learned to write modules.

So here is a template for stealing routines from existing code and putting them into a procedural module.

  1. Decide what module the routine belongs in. If the answer is none that you have, start a new module.
  2. To start a new module create a file Foo.pm, and in it type the following template:
    package Foo; use Exporter; @ISA = 'Exporter'; @EXPORT_OK = qw(); use strict; # Your subs will go here. 1;
    The word "Foo" must match the module name (with the usual conversion between the package separator :: and the path /).
  3. Copy the subroutine into the module.
  4. Put the name of the subroutine in the @EXPORT_OK list.
  5. Comment out the original subroutine.
  6. Put use Foo qw(sub_name); back in the original script.
  7. Test. (Often the subroutine will have dependencies that also need to go into the module.)
  8. Roll out.
As for the location of modules, that is what lib is for. I personally have a custom configuration loader that will (among other things) transparently do the right lib invocations for me. In development pick up my copy of stuff in CVS. In production pick up the production copy of the same. The exact layout and development of that kind of functionality is a site decision. (Having this available has made testing and development much easier...)

In reply to Re (tilly) 2: Simpler alternative to modules by tilly
in thread Creating module files from subroutines by Anonymous Monk

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.