Basically the file UtilLib.pm will look like this:
# require outside the package so that its functions are # NOT imported into the UtilLib namespace, so that other # libraries can use them. require mysql_wrapper_lib.pl; package UtilLib;
(BTW your filename for require must be quoted.)
This is the worst possible idea. Why? Because mysql_wrapper_lib.pl will be required only once, and it'll be imported into the package that was the first to use your module. It is a moving target, in general you have no idea where that will be. A very simple example:
# File mysql_wrapper_lib.pl $foo = 123; 1;
Now with the code:
package One; use UtilLib; package Two; use UtilLib;
Where will your $foo end up? In package One, and not in package Two. Without the use statement in package One, or with package One loaded later, it will end up in package Two.

Now imagine that package One and package Two are in two different modules, and loaded from various places. What package will $foo eventually end up in? Well, it'll probably be either in package One or in package Two, but which one?

And that's why use got support for import, so the same variables/functions can be aliased in any package you like. Problem solved.

# File Mysql_wrapper.pm package Mysql_wrapper; use base 'Exporter'; @EXPORT = qw($foo); $foo = 123; 1;
package One; use Mysql_wrapper; package Two; use Mysql_wrapper;
So now where will $foo end up in? Guess what: both in package One and in package Two. $One::foo and $Two::foo will actually point to the same variable, that's why they are called "aliases".

p.s. It's not absolutely necessary to use Exporter, but it saves you from having to do the manual work of actually importing the variables yourself. That would end up to be a copy/paste job anyway, as it's not code that is easy to remember. Just to show it can be done (after all, we're here to learn right? :) ) here is some code that would do just that:

package Mysql_wrapper; $foo = 123; sub import { my $pkg = caller; no strict 'refs'; *{"$pkg\::foo"} = \$foo; # and the same for other variables... } 1;

In reply to Re: Good Module Design by bart
in thread Good Module Design by shemp

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.