"use base" revelation

rhesa on 2005-06-20T11:33:18

I always lived with the idea that "use base" was introduced with perl 5.6.0, and that we had to write

  require BaseClass;    # Thanks, merlyn :)
  use vars qw/@ISA/;
  @ISA = qw/BaseClass/;


to be compatible with older perls.

Today I learned (http://search.cpan.org/dist/base/lib/base.pm>) that "use base" was introduced with Perl 5.004_04!
Is this an odd case of cargo cult? Does it mean we can stop doing the "use vars '@ISA'" thing now?

Who wants their OO code to be compatible with perls older than 5.005?

And where did I get this notion from that it only became available in 5.6?

Sometimes it seems that experience just means you're set in your ways....


Missing the third part of that "use base" equiv...

merlyn on 2005-06-20T11:58:21

use base qw(Foo Bar);
is really the combination of:

require Foo; require Bar; use vars qw(@ISA); @ISA = qw(Foo Bar);

Don't forget those requires!

Re:Missing the third part of that "use base" equiv

rhesa on 2005-06-20T12:07:55

You're right, of course. But my question was, do we need to remember that? Can't we just "use base" for the rest of our lives and forget about that archaic way of subclassing?

use base is problematic

bart on 2005-06-20T14:34:45

base.pm is a mess. Occasionally it doesn't succeed in actually loading the parent module, and due to the way it's set up, it may not even complain about it too loudly, or at least not loud enough — maybe just a warning.

I don't trust it. I prefer to call require or use myself, and consequentially, setting @ISA by hand.

Re:use base is problematic

rhesa on 2005-06-20T15:11:52

If base.pm really is a mess, then surely it wouldn't have been included in the core distribution.

No offence, but have you sent in a bug report with a patch, or even a failing test? I'm sure you must have hit an odd corner case. It's in the core, it's in broad use, so it must be okay for the majority of uses.

Re:use base is problematic

chromatic on 2005-06-20T16:12:47

If base.pm really is a mess, then surely it wouldn't have been included in the core distribution.

I wish that were true, but sadly, it's not. Quite a few core modules have big, big messes inside.

Re:use base is problematic

rhesa on 2005-06-20T16:36:33

Well, thank you for bursting my bubble on that one ;)

Any chance you could respond to my original questions? I'm really curious about it.

Re:use base is problematic

bart on 2005-06-20T18:31:21

The problem, IMO, is the fact the base tries to solve two different problems in one swoop, with identical syntax. As a result: loading the module is optional. And all too often, it goes wrong. You get two types of cases where it goes wrong:
  1. Trying to load a module (and failing) where it shouldn't even have tried
  2. Not trying to load a module where it should have loaded it
If you could steer base telling it exactly where you want it to load a module or not, then there would be far less of a problem. I personally would have thought of a "+" or "-" prefix in front of the module name, for example:
use base '+Foo';   # requires the module
use base '-Foo';   # doesn't require the module

Re:

Aristotle on 2005-06-20T17:04:18

I think you are confusing this with something else. The thing that was introduced with Perl 5.6 was our, so you can say

require Foo;
our @ISA = qw( Foo );

instead of

use vars qw( @ISA );
require Foo;
@ISA = qw( Foo );