Heh. I was the one that suggested you use a tied hash.

You need to be careful about one point: "exporting" is not the same as "exposing an interface". In Perl has, "export" has a precise meaning (to introduce symbol table entries into the caller's main package), whereas an interface style is strictly a design decision. The two are related, but not the same thing.

Let me elaborate. Say you choose to present a function-oriented interface. You can choose to export the functions (using the standard Exporter module as your example code is doing). That means when I use the module, I get those sub symbols entered in my main package, potentially clashing with my own variable names or those from other packages.

I can keep the function oriented interface but not export any names by using an object-oriented interface. So, for example, instead of this:

use AIX::Sysinfo; ... my $host = hostname(); # sub exported from module my $version = system_version(); # sub exported from module
I could have this:
Use AIX::Sysinfo; ... my $sys = AIX::Sysinfo->new(); my $host = $sys->hostname();
The difference between the first and second versions (as far as exported symbols is concerned) is that in the first example I have no control over clashing names like hostname and system_version, whereas in the second, name clashes aren't possible (well, except for the module name itself...) You notice that $sys, my handle to the package, is a lexical now.

The same consideration applies to an interface exposed through variables, in this case a hash. I can choose to export the hash from my code, polluting my caller's namespace, or I can allow the caller to choose names by tying. Here's the first way:

use AIX::Sysinfo_var; # making this up my $hostname = $sysinfo_hash{hostname}; # making this up my $version = $sysinfo_hash{aix_version};
And here's the second:
use AIX::Sysinfo_tie; ... tie %sysinfo, 'AIX::Sysinfo'; my $hostname = $sysinfo{hostname};
Again, the difference is that the first module introduced the %sysinfo_hash into the caller's namespace, but the second module introduced no new symbols. Nonetheless, both are exposing a variable-oriented interface.

HTH


In reply to Re: should a module export FUNCTIONS or VARIABLES? by VSarkiss
in thread should a module export FUNCTIONS or VARIABLES? by blueflashlight

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.