Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

"use base" or @ISA

by uwevoelker (Pilgrim)
on Aug 17, 2004 at 14:58 UTC ( #383693=perlquestion: print w/replies, xml ) Need Help??

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

use base 'Exporter';
and
@ISA = qw(Exporter);
seem to be the same thing. I was thinking 'use base' was introduced later and should be prefered. But even never code often uses @ISA.

Which one should be prefered and why?

Thanks, Uwe

Replies are listed 'Best First'.
Re: "use base" or @ISA
by davido (Cardinal) on Aug 17, 2004 at 15:06 UTC

    Not exactly the same thing. @ISA = qw/Exporter/; doesn't perform a 'require Exporter;'.

    From the base POD: use base; "is rougly similar to":

    BEGIN { require Foo; require Bar; push @ISA, qw(Foo Bar); }

    I like use base; also because it runs under strictures without explicitly calling "use vars qw/@ISA/;", or declaring "our @ISA...". ...just a minor improvement, IMHO, but I prefer to use it.


    Dave

Re: "use base" or @ISA
by ikegami (Patriarch) on Aug 17, 2004 at 15:16 UTC

    As an aside, I hate inhererting from Exporter. My class does NOT have an ISA relationship with Exporter! (And inheriting an autoload? ouch!) Below is an alternative.

    Instead of:

    use vars qw( @ISA @EXPORT_OK %EXPORT_TAGS ); BEGIN { @ISA = qw( Exporter ); @EXPORT_OK = qw( ... ); %EXPORT_TAGS = ( ... ); # if necessary use Exporter; }

    try:

    use vars qw( @EXPORT_OK %EXPORT_TAGS ); BEGIN { @EXPORT_OK = qw( ... ); %EXPORT_TAGS = ( ... ); # if necessary require Exporter; *import = \&Exporter::import; }

    Update: s/module/class/;

      My module does NOT have an ISA relationship with Exporter!

      Really? So, how would you explain the relationship? Your module exports; it is an exporter.

        My module does NOT have an ISA relationship with Exporter!

        Really? So, how would you explain the relationship? Your module exports; it is an exporter.

        I would say that the module "delegates to Exporter", or "uses Exporter".

        Delegation and composition are perfectly valid implementation mechanisms, and there's no reason to view inheritance as the one true way of sharing functionality.

        oops, that was a typo or a freudian slip or something. I meant to say my class isn't an exporter. A dog (for example) is an animal, but a dog isn't an exporter (although the package in which dog is defined might be). That's just my way of looking at things, so use the style you like the most.

      I use Exporter::Lite. It is much lighter and has a clean interface (its import() is simply exported). It doesn't support tags, but then I never needed them anyway.

      Exporter::Tidy is another alternative if you need advanced features (some of which Exporter doesn't even offer.)

      Makeshifts last the longest.

      Exporter has:

      package YourModule; use Exporter qw( import );

      under the heading Exporting without inheriting from Exporter.

      Regards,

      PN5

Re: "use base" or @ISA
by borisz (Canon) on Aug 17, 2004 at 15:04 UTC
    I use both from time to time. A difference is that use base 'Exporter'; is like
    require Exporter; push @ISA, 'Exporter';
    so you may save a line. For all the differences read perldoc base.
    Boris
Re: "use base" or @ISA
by dragonchild (Archbishop) on Aug 18, 2004 at 02:58 UTC
    There is a subtle difference between the following:
    use base 'Foo'; ---- use Foo; our @ISA = qw( Foo );

    I always use the latter, because 'use base' does a require, not a use. This means that Foo's import() isn't called. Most people would consider this to be a good thing, but I don't like limiting the options for the modules I work with. This is Perl, after all.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: "use base" or @ISA
by adrianh (Chancellor) on Aug 18, 2004 at 11:15 UTC
    Which one should be prefered and why?

    I prefer use base for three reasons:

    • To me, it expresses my intent more clearly. I am using this module as a base class. When I have separate use/require and @ISA and have to expend that little bit more effort to associate the two.
    • Less typing.
    • Since use base does not die if the specified module cannot be required I don't have to change my code when I move between inlined packages and external modules

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://383693]
Approved by Fletch
Front-paged by xdg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2022-05-25 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?