开发者

Why is constant not automatically inherited?

开发者 https://www.devze.com 2023-03-30 05:13 出处:网络
As we know constant in Perl is just sub, but why are the开发者_开发问答y not inherited?As the matter of fact, they are:

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

精彩评论

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