Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

difference between "use base" and @ISA?

by markjugg (Curate)
on Aug 25, 2005 at 15:41 UTC ( [id://486593]=perlquestion: print w/replies, xml ) Need Help??

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

Before today, I thought these meant the same thing, with the @ISA style being older and more compatible:

use Foo; @ISA = ('Foo');

and...

use base 'Foo';

However, only the latter works with CGI::Application::Plugin::AutoRunmode, meaning there is something I don't fully understand here. The importing must be happening differently, but the docs for 'base' and 'Exporter' don't clarify the difference for me.

Replies are listed 'Best First'.
Re: difference between "use base" and @ISA?
by CountZero (Bishop) on Aug 25, 2005 at 15:57 UTC
    From the docs:

    use base qw(Foo Bar);

    Roughly similar in effect to

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

    Will also initialize the fields if one of the base classes has it. Multiple Inheritence of fields is NOT supported, if two or more base classes each have inheritable fields the 'base' pragma will croak. See fields, public and protected for a description of this feature.

    When strict 'vars' is in scope, base also lets you assign to @ISA without having to declare @ISA with the 'vars' pragma first.

    If any of the base classes are not loaded yet, base silently requires them (but it won't call the import method). Whether to require a base class package is determined by the absence of a global $VERSION in the base package. If $VERSION is not detected even after loading it, base will define $VERSION in the base package, setting it to the string -1, set by base.pm.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: difference between "use base" and @ISA?
by BaldPenguin (Friar) on Aug 25, 2005 at 16:48 UTC
    Also take a look at previous discusions:
    Updated:
    The big difference between the two, in my opinion, is the initialization and use of %FIELDS that base uses, and is not used pushing to @ISA, CGI::Application::Plugin::AutoRunmode may need the %FIELDS variable.

    Don
    WHITEPAGES.COM | INC
    Everything I've learned in life can be summed up in a small perl script!
Re: difference between "use base" and @ISA?
by friedo (Prior) on Aug 25, 2005 at 15:56 UTC
      @ISA = ('Foo'); will destroy whatever was already in @ISA and will surely break a lot of other things depending on that array.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

        My understanding has always been that @ISA is a package global, so the only way it would have something in it is if it were set elsewhere in the same package, which would be a pretty strange thing to do. In other words, it's safe to overwrite it in your own package. I think friedo's code is fine, but I'd be happy to learn otherwise if there's something I'm missing.
Re: difference between "use base" and @ISA?
by davidrw (Prior) on Aug 25, 2005 at 16:57 UTC
Re: difference between "use base" and @ISA?
by Tanktalus (Canon) on Aug 26, 2005 at 04:19 UTC

    Mark, there's nothing I see in CGI::Application::Plugin::AutoRunmode that points to use base working or not working that I can see. Perhaps a snippet of your module that exhibits the same problem, and a better description of the symptoms you're facing may be in order? You may have misdiagnosed the problem here. Not that you aren't getting good answers to the question you asked, but the question may not be actually related to your problem.

    Thanks,

      To be clear, my problem is not getting my application to work. "use base" works for me. I want to learn the about the differences to make intelligent choices in the future. And thanks to the many helpful responses, I think I've got a chance of figuring this out. Thanks all!

        Ok, now I'm confused. In your OP, you said:

        However, only the latter works with CGI::Application::Plugin::AutoRunmode
        which implies to me that something, well, isn't working when you don't "use base ...". I'm curious as to what it is that isn't working when you "use CGI::Application::Plugin::AutoRunmode; push @INC, qw(CGI::Application::Plugin::AutoRunmode);" - what the symptoms are, etc., that "use base qw(CGI::Application::Plugin::AutoRunmode);" fixes?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-23 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found