On Fri, 23 Feb 2001 17:13:45 -0800, Bruce Van Allen wrote:

>1. Don't put the same variable (e.g., @EXPORT_OK) in a 'use vars ...' 
>declaration more than once in a module.

Not a bad idea. The reason why he needs it more than once, is because
this "use vars" thing is in a block. Just put it upfront, not wrapped in
ANY block. "use vars" is executed as soon as it is parsed, anyway.

>2. Why use the BEGIN block? Try without it. Many initializations work 
>perfectly well without it.
>
>3. Bring in the import capabilities of Exporter.pm by *inheritance*, 
>that is, by including the statement '@ISA = qw(Exporter)'. You were 
>doing this but also using the statement 'use Exporter ();' -- lose 
>the latter.

No, you still have to load the file. "require Exporter;" is the normal
way.

There is a module that does both set @ISA and load the module file: "use
base".

Anyway: the way this normally works is that your module inherits from
Exporter. Your module doesn't have an "import" sub (it really
shouldn't), so it's the one from Exporter that gets used instead. And
that one checks the variables @EXPORT and @EXPORT_OK of your package,
the ones that got just defined, and does the requested aliasing for you.

If you don't know for sure what alasing is: it is making sure that $x
and $y reference the same variable, in this example a scalar. It can be
done with:

        *x = \$y;

So that:

        $x = "Hi!";
        print $y;

prints "Hi!".

That goes for ANY kind of variiable, subs too. It also works with
variables of the same name in a different package:

        *Foo::x = \$Boo::x;

This is the normal situation for "importing": so that subs and variables
in your module package become available, unqualified, in your code, as
if they were defined in your own package.

Check the docs (and implementation) for Exporter for more details.

-- 
        Bart.

Reply via email to