Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between `use base` and @ISA in Perl?

Tags:

oop

perl

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.

like image 725
aidan Avatar asked Sep 04 '09 10:09

aidan


3 Answers

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.

like image 126
Chas. Owens Avatar answered Oct 20 '22 09:10

Chas. Owens


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

like image 21
Alan Haggai Alavi Avatar answered Oct 20 '22 07:10

Alan Haggai Alavi


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.

like image 36
Stephen Sorensen Avatar answered Oct 20 '22 08:10

Stephen Sorensen