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

Dear monks, Is there a good resouce for module inheritance? Have googled plenty but can't find any good enough. I don't have my basics right. Works partially for me. There is a module, say Generic.pm which is being inherited in Module1 successfully using
use base qw/Generic/;
and Generic.pm contains
require Exporter; our @ISA = ("Exporter"); our @EXPORT_OK = qw/sub1 sub2/;
Now I wish to inherit Module1 in Module2, so I say in Module2.pm
use base qw/Module1/;
but this gives me errors everytime I try to call a Module1 function. Do I need to include the require Exporter stuff again in Module1? Thanks. Fixed. Added the Exporter stuff in Module1 and works just fine. However, had to add ALL the subs in Module1 in its @EXPORT. Is there an easier way so that it exports all its subs by default? Thanks mate.

Replies are listed 'Best First'.
Re: Module inheritance
by dragonchild (Archbishop) on Apr 15, 2005 at 04:29 UTC
    Yes. The functions you define in Module1 live in Module1's namespace. To call them from Module2, you either have to
    1. Module1::function()
    2. Do the Exporter stuff
    3. Treat Module2 (and Module1) as classes with objects

    They're all good solutions, but to different problems. Which solution you use depends on which problem you have.

Re: Module inheritance
by QM (Parson) on Apr 15, 2005 at 21:26 UTC
    Don't know if you told dragonchild about your reply -- unless he comes back here, he'll miss your followup question.
    Added the Exporter stuff in Module1 and works just fine. However, had to add ALL the subs in Module1 in its @EXPORT. Is there an easier way so that it exports all its subs by default?
    See Exporter:
    package MyPackage; ... @EXPORT = qw(sub1 sub2);
    but this can lead to namespace pollution. I'd prefer @EXPORT_OK and %EXPORT_TAGS in most cases.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of