I think you will find it useful to become acquainted with Exporter, which is one of Perl's standard mechanisms for facilitating code sharing (basically it streamlines the process of importing identifiers from different namespaces, so that you don't need to prepend their package names).

Other than this, I couldn't answer your question in a node of "reasonable length" (though a more knowledgeable monk might; after all, economy is a mark of mastery). I think that a book-length treatment of modules and/or OO in Perl (like the Alpaca book or Object Oriented Perl) would be the place to look for answers.

As far as your sharing your config data, if I wanted to keep things as simple as you have them, one possibility (which is similar to wazoox's proposal but without the singleton idea from OO design) is to use a hash to hold all the config info, along with a barebones accessor for it.

# home.conf package MyPackage; my %config = ( WIKI_TITLE => 'MyWiki', DEFAULT_TARGET => 'WelcomeToMyWiki', DEFAULT_ACTION => 'view', # etc. ); sub get_config { return %config; } 1;

The code that needs this config info would then do this:

require 'home.conf'; # I'm not crazy about this name # ... my %config = MyPackage::get_config(); # copy of original hash my $frobozz = foobar( $config{ WIKI_TITLE } );
A simple variant of this would be to define a generic accessor for configuration parameters:
package MyPackage; my %config = ( # as before ); sub config { my $config_param = shift; if ( defined $config{ $config_param } ) { return $config{ $config_param }; } else { die "Unknown configuration parameter: $config_param"; } } 1;
...and the calling code would be something like:
require 'home.conf'; # ... my $frobozz = foobar( MyPackage::config( 'WIKI_TITLE' ) );
If you want to avoid the nuisance of prepending 'MyPackage::' before the name of the config function without fiddling with Exporter, you can do this:
require 'home.conf'; # ... *config = \&MyPackage::config; # make an alias my $frobozz = foobar( config( 'WIKI_TITLE' ) );
TMTOWTDI, indeed... (In fact, I suspect that better ways to do this without introducing too much complexity will not be long in coming.)

BTW, all the code above is largely untested. I'm presenting it as code as a way to convey the basic ideas. Make sure you test whatever you end up using.

the lowliest monk


In reply to Re^3: Packaging up my code sensibly by tlm
in thread Packaging up my code sensibly 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.