Monks,

I'm fairly sure this is something that comes up fairly regularly; however, I think as with Perl, people's tastes with regard to it change fairly regularly as well.

A couple of nodes (fairly) recently have looked at the namespace issue from both sides, from an implementation point of view, but nothing as yet has come up regarding the more theoretical side, which is what I'm interested in here.

My original impression, from reading a number of previous posts here, and from looking through the Camel and Black Book, was that automatically importing subroutines, variables and other symbols from a module to user-code was considered a Bad Thing, something to be avoided.

Having said that, though, I can see that it can, on occasion, be a very useful thing if, for example, importing a module exports a set of common subroutines, or secondary modules, to the user's namespace, saving a user of the module from having to figure out exactly which methods or modules in the suite he actually needs.

Going from one extreme to the other, though, it's also (in my opinion) a very nice thing to be able to call, for example, Date::Calc with a string of simply the subs you actually want to use. For larger, more complex modules, I can't help but think that it makes more sense to import what's needed rather than everything that's available.

I think that my personal preference/solution when I'm both writing and using module code is to have an option of either: if the module is used without arguments, or with an :all flag, a "default" set of subs is exported, but if a list of specific subs is given, only those are imported into the module user's namespace. This, I think, gives maximum flexibility for larger apps, and for future expandability of both the module and for the code using that module.

I'm interested to know what others think of this: is there a set "right or wrong" with regard to something like this? I tend to strive to not export anything I don't specifically need: do people think this the best way to go?

Any comments and opinions are welcome :)

-- Foxcub
A friend is someone who can see straight through you, yet still enjoy the view. (Anon)


In reply to Namespace Pollution by Tanalis

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.