开发者

Why is constant not automatically inherited?

As we know constant in Perl is just sub,

but why are the开发者_开发问答y not inherited?


As the matter of fact, they are:

use strict; use warnings;

package Father;
use constant CONST => 1;

package Child;
use base 'Father';
sub new { bless {}, shift }

package main;
my $c = Child->new;
print $c->CONST;        # 1
print CONST();          # undefined subroutine


Methods are inherited, functions are not. If you want to inherit the constant, you'll need to call it like a method.

$self->FOO

or

__PACAKAGE__->FOO

That said, you should be importing constants, not inheriting them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜