http://qs321.pair.com?node_id=473917

murugu has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

While writing modules(with out OOP concepts) what is the need for

require Exporter; @ISA=("Exporter");

Why do we need to inherit Exporter in our current package?

Can we use "use Exporter" instead of both the lines above? Without "Import" how will be the symbols imported?

By writing the 2 lines above does the symbols present in the current package will be automagically exported to the program where we use this package? If so how?

Thanks in advance.

Regards,
Murugesan Kandasamy.

Replies are listed 'Best First'.
Re: Module Loading via Exporter doubt.
by Joost (Canon) on Jul 11, 2005 at 13:40 UTC
    • There usually isn't any need for using Exporter in an OO module.
    • You can use use base 'Exporter'; instead of the 2 lines. Just use Exporter; will not set up the inhertance of exporter. The normal use of Exporter is that your module inherits the import() method from it, even if the rest of the modules usually isn't OO.
    • You need to setup the @EXPORT, @EXPORT_OK and/or %EXPORT_TAGS package variables in order to export anything. Exporter's import() method will use those to determine what to export when your module is use()d. This is explained in the Exporter documentation.

Re: Module Loading via Exporter doubt.
by castaway (Parson) on Jul 11, 2005 at 13:30 UTC
    Inheriting from Exporter means that your module gains a function which is defined in the Exporter package, the "import" function. This means that anyone "using" your package will call Exporters import, to import your exported functions.

    You don't need to "use" Exporter, since you do not need any functions from Exporter in your modules namespace.

    No, no symbols in your package are automatically exported, you need to tell it exactly which ones you want to export, using the @EXPORT_OK variable.

    In short, when someone "uses" your package, which is in turn inheriting from Exporter, the import function is called, which looks at your @EXPORT_OK variable, to decide which symbols to import into the calling codes namespace.

    HTH, C.

      To be complete, I'd like to add: Exporter looks not only at @EXPORT_OK, but also @EXPORT and %EXPORT_TAGS. My personal preference is to use @EXPORT_OK rather than @EXPORT because I feel is is more polite. %EXPORT_TAGS can also be quite useful when properly used.

      See Exporter


      They say that time changes things, but you actually have to change them yourself.

      —Andy Warhol

Re: Module Loading via Exporter doubt.
by broquaint (Abbot) on Jul 11, 2005 at 13:32 UTC
    Why do we need to inherit Exporter in our current package?
    This is so the module can inherit Exporter's import method.
    By writing the 2 lines above does the symbols present in the current package will be automagically exported to the program where we use this package? If so how?
    To automagically export symbols Exporter uses the package variables @EXPORT, @EXPORT_OK and %EXPORT_TAGS from the class that inherits from it and exports as appropriate.
    HTH

    _________
    broquaint

Re: Module Loading via Exporter doubt.
by Fletch (Bishop) on Jul 11, 2005 at 14:25 UTC
Re: Module Loading via Exporter doubt.
by tlm (Prior) on Jul 12, 2005 at 01:15 UTC

    For the sake of completeness, you can just grab Exporter::import without inheriting it:

    package Foo; require Exporter; *import = \&Exporter::import;

    Update: I just learned from tilly's latest meditation that there is now a simpler way to achieve the same end result:

    package Foo; use Exporter 'import';

    the lowliest monk

Re: Module Loading via Exporter doubt.
by garrison (Scribe) on Jul 12, 2005 at 05:52 UTC
    Perhaps I'm misreading "with out OOP concepts" but if one isn't concerned with OO or inheritance then Exporter isn't needed at all, though it can certainly make life easier.

    Naturally this means prepending the package namespace as in:
    my $foo = $Some::Package::HASH{'bar'};
    -gh