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.
精彩评论