in reply to should a module export FUNCTIONS or VARIABLES?
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:
I could have this:use AIX::Sysinfo; ... my $host = hostname(); # sub exported from module my $version = system_version(); # sub exported from module
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.Use AIX::Sysinfo; ... my $sys = AIX::Sysinfo->new(); my $host = $sys->hostname();
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:
And here's the second:use AIX::Sysinfo_var; # making this up my $hostname = $sysinfo_hash{hostname}; # making this up my $version = $sysinfo_hash{aix_version};
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.use AIX::Sysinfo_tie; ... tie %sysinfo, 'AIX::Sysinfo'; my $hostname = $sysinfo{hostname};
HTH
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: should a module export FUNCTIONS or VARIABLES?
by blueflashlight (Pilgrim) on Jul 02, 2001 at 22:31 UTC |