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

Dear monks,

I've taken to using base.pm for declaring my inheritance trees (i.e. use base qw(Parent);), so now I'm wondering what magic I'd lose by doing it that way rather than using @ISA. What difference does it make?

Thanks!

Replies are listed 'Best First'.
Re: @ISA vs. base
by rinceWind (Monsignor) on Jul 25, 2005 at 14:37 UTC

    The pragmatic core module base.pm merely provides a wrapper to do the convenient stuff, which includes setting @ISA. It also uses the module(s) for you. There is also some code to deal with use fields. All of this takes place in an import sub, hence at BEGIN time.

    The only think you might lose with use base, is backward compatibility with ancient perl versions that did not have it.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Re: @ISA vs. base
by anonymized user 468275 (Curate) on Jul 25, 2005 at 14:40 UTC
    Using base.pm has the following differences from just pushing the classes onto @ISA:

    1) It initialises the %FIELDS hash if one of the base classes has it.

    2) Note that because of this multiple inheritance is not supported when using base.pm (Update: this restriction only applies when also using fields -- see link below.)

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

    See also http://www.perl.com/doc/manual/html/lib/fields.html

    One world, one people

      Correction - multiple inheritence is supported, multiple inheritence from two classes which use fields is not (one parent with, the rest without is OK, IIRC).

      By the way this limitation should go away in perl 5.10 since 5.9 no longer uses pseudohashes for this stuff.

      -nuffin
      zz zZ Z Z #!perl
Re: @ISA vs. base
by ikegami (Patriarch) on Jul 25, 2005 at 14:35 UTC
    You don't lose anything, except the need to require/use the parent module. In other words, it sets up @ISA and does even more.
Re: @ISA vs. base
by siracusa (Friar) on Jul 25, 2005 at 16:07 UTC

    I tend not to "use base" unless I actually want the special features that it provides (i.e., the "use fields" support). If not, I do it the old-fashioned way. I feel like that's more "honest," even if it takes an extra line of code. And hey, it also saves loading one module.

      I'm not sure I get this. I tend to go the other way: I use every single module I can, unless it doesn't work for my scenario. The advantage is that as they add new features, functionality, and speed, I get them for free.

      For example, rather than about five lines of code to copy a file, I use File::Copy and use its copy function. That way, on platforms where there is a system copy, I get that faster copy for free. Attributes are copied. All those bonus functionalities are handled for free. I only don't use File::Copy where it doesn't work - as in string refs.

      I don't, offhand, know of any scenario where base doesn't work, although I'm sure tye will enlighten me ;-), so I continue to use it as a preference over all others until then.

        Don't get me wrong, I'm all for using modules. It's just that base.pm is a bit of a special case in that, when I see it at least, I immediately think that the package from which it's "use"d also uses fields.pm.

        If base.pm was just syntactic sugar for pushing onto @ISA, I'd be more inclined to use it. But its usage seems (to me anyway) to imply that fields.pm is also in the mix. Even if it's not, there's no way to tell from the "use base" line alone.

        I tend not to "use base" so no one will erroneously suspect that I'm using fields.pm and (probably more importantly) to avoid implying that one needs to "use base" if in order to subclass my class cleanly.

Re: @ISA vs. base
by adrianh (Chancellor) on Jul 26, 2005 at 11:05 UTC
    I'm wondering what magic I'd lose by doing it that way rather than using @ISA. What difference does it make?

    No losses, apart from backwards compatibility with versions of Perl before 5.005. I prefer use base since it allows me to avoid differences between inlined packages and ones in separate files.

Re: @ISA vs. base
by Anonymous Monk on Jul 25, 2005 at 23:15 UTC