: >From: David McDivitt <
[email protected]>
: >
: >I am having difficulty understanding packages. I made a package, and
: >figured out how to make variables visible to modules using it without
: >qualifying with the package name, but exposing package subroutines the
: >same way is a bit confusing. I read several things. I do not understand
: >export syntax and how to set that up. Would someone please post an
: >example package having one routine called "init", and make the init
: >routine visible to any module using it without having to say
: >PACKAGENAME::init() in that module. Thanks
:
: Below is how I resolved my problem. If I find my own answer I like to put my
: own answer back in the forum.
Very nice. Not a behavior that needs excusing.
: In the modules using this package I give two
: lines:
:
: use DGM;
: my @ISA = qw(DGM);
That doesn't do what you think it does. @ISA must be a package variable
for inheritance to work.
our @ISA = qw(DGM);
But that only makes sense if DGM.pm is a class you wish to inherit from.
If it's a class, why should it be exporting symbols?
: I got confused with the export stuff and did not understand what was going
: on. I had however read about @ISA when reading about classes. It seems
: placing "use export"
"use Exporter;"
: in the package does nothing more than avoid one extra
: line of code in the main application, but ends up pulling in another whole
: module, too.
Well, that's not a very good reason not to use Exporter.pm .
The modules that get loaded are small, and if your app achieves any kind
of complexity, they're probably already being loaded by some other
module your code requires.
A good reason not to use Exporter.pm would be that it simply can't do
what you want. In this case, it's perfectly suited to the task.
Getting basic functionality from Exporter.pm is dead simple.
require Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw($foo @bar %baz &qux);
: It was the "use export" which confused me, and I thought it was
: a necessary thing.
Not necessary, but it's very easy to use once its function is
understood.
: With what I have, two variables are made visible to the
: main application without having to qualify with package name, and all
: functions are visible.
Visible in main:: , yes. And if main:: is not the package where the
use() occurred, main:: gets the extra baggage instead of the package
that asked for it.
Exporter.pm , on the other hand, exports symbols to the package where
the use() occurrs. Even better, the programmer can control which
symbols get exported without altering the module at all.