I personally like this approach. It also lets application developers using your module do something like this:

use JacksLumber; $JacksLumber::basedir = '/some/other/dir'; # override default

This is simple and intuitive and avoids having the user of your module learn your configuration interface if he wants to make changes to these simple variables.

HOWEVER, what if your configuration is complex? What if it's structured? What if you can't predict what the variable names will be while you're coding? You don't want to pollute your module's namespace with an unknown list of variables, so you gotta go with a hash (and perhaps a complex data structure).

I've used a few different approaches, depending on the project. I've exported a %CONFIG hash (or a hashref), I've created an AUTOLOAD in a ::Config module that let me reference configuration as JacksLumber::Config->key and other things. I still haven't come across a technique that I liked that was well-suited to any project, and invariably I start with a simple approach that ends up more complicated, making my configuration approach too constraining. For example, if 'dir' was a variable under a 'base' hierarchy, I'd end up with something unmaintainable like JacksLumber::Config->base->{dir}. How intuitive is that? With the use of things like Class::Struct and similar modules, you might be able to make that a little more readable, but it's still ugly in my eyes.

If your "global" variables might change between invocations of your package methods/functions, you might consider wrapping your configuration in an object, and set up object methods (or a generic 'get'/'set' pair) to allow you to set/fetch your "global" variables. In other words:

# Go from this: use JacksLumber 'some_func'; $one = some_func(1, 2, 3); $JacksLumber::alternate_behavior = 1; $two = some_func(1, 2, 3); # To this: use JacksLumber; my $normal = new JacksLumber; my $alt = new JacksLumber(alternate_behavior => 1); $normal->some_func(1, 2, 3); $alt->some_func(1, 2, 3);

But generally, the simplest thing to do might just be a hash or a hashref in your module's root namespace for complex configuration, or just some package variables if you can get by with that.

Just my two cents...


In reply to Re: Re: Coding Perl With Style by Fastolfe
in thread Coding Perl With Style by Dogma

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.