Perl中的"use base"和@ISA有什么区别? [英] What's the difference between `use base` and @ISA in Perl?
问题描述
我想制作一个扩展DBI的单例类.我应该做这样的事情吗?
I want to make a singleton class which extends DBI. should I be doing something like this:
use base 'Class::Singleton';
our @ISA = ('DBI');
或者这个:
our @ISA = ('Class::Singleton', 'DBI');
还是其他?
不太清楚'use base'和'isa'之间的区别是什么.
Not really sure what the difference between 'use base' and 'isa' is.
推荐答案
@ISA
的典型用法是
package Foo;
require Bar;
our @ISA = qw/Bar/;
base
和parent
编译指示均加载请求的类并修改@ISA
使其包含:
The base
and parent
pragmas both load the requested class and modify @ISA
to include it:
package Foo;
use base qw/Bar/;
如果要多重继承,可以为base
或parent
提供多个模块:
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");
parent
编译指示从Perl 5.10.1开始是新的,但是如果您具有较旧的Perl版本,则可以从CPAN安装该编译指示.之所以创建它,是因为base
杂项由于堆积在其中的残骸"而变得难以维护.您应该不会在两者的基本用法上看到差异.
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.
这篇关于Perl中的"use base"和@ISA有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!