9

I want to make a singleton class which extends DBI. should I be doing something like this:

use base 'Class::Singleton';
our @ISA = ('DBI');

or this:

our @ISA = ('Class::Singleton', 'DBI');

or something else?

Not really sure what the difference between 'use base' and 'isa' is.

serenesat
  • 4,611
  • 10
  • 37
  • 53
aidan
  • 9,310
  • 8
  • 68
  • 82

4 Answers4

8

The typical use of @ISA is

package Foo;

require Bar;
our @ISA = qw/Bar/;

The base and parent pragmas both load the requested class and modify @ISA to include it:

package Foo;

use base qw/Bar/;

If you want multiple inheritance, you can supply more than one module to base or parent:

package Foo;

use parent qw/Bar Baz/; #@ISA is now ("Bar", "Baz");

The parent pragma is new as of Perl 5.10.1, but it is installable from CPAN if you have an older version of Perl. It was created because the base pragma had become difficult to maintain due to "cruft that had accumulated in it." You should not see a difference in the basic use between the two.

Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
4

I think you should use the parent pragma instead of base as has been suggested in perldoc base.

Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
  • 1
    Is 'parent' new in 5.10? It must be, as it's not in my 5.8 documentation. – Ether Sep 04 '09 at 16:56
  • I am not sure. However, quoting Chas's statement: "... The parent pragma is new as of Perl 5.10.1, but it is installable from CPAN if you have an older version of Perl. ..." – Alan Haggai Alavi Sep 04 '09 at 17:11
3

from base's perldoc...

package Baz;

use base qw( Foo Bar );

is essentially equivalent to

package Baz;

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

Personally, I use base.

Stephen Sorensen
  • 11,455
  • 13
  • 33
  • 46
0

If you want to inherit parent class then you would need to load it and modify @ISA. This both the steps are taken care of when you use Base. Base loads the module for you and modify @ISA accordingly. Otherwise you would need to modify @ISA and load the parent module on your own.

Both of the following are equivalent:

use base "Parent";

and

require Parent;
@ISA = ("Parent");
chammu
  • 1,275
  • 1
  • 18
  • 26