Perl在现代Perl中定义类
示例
尽管可用,但在现代Perl中不建议从头开始定义类。使用提供更多功能和便利的辅助OO系统之一。这些系统包括:
Moose-受Perl6OO设计启发
Class::Accessor-驼鹿的轻巧替代品
Class::Tiny-真正的最小类建造者
驼鹿
package Foo;
use Moose;
has bar => (is => 'ro'); # a read-only property
has baz => (is => 'rw', isa => 'Bool'); # a read-write boolean property
sub qux {
my $self = shift;
my $barIsBaz = $self->bar eq 'baz'; # property getter
$self->baz($barIsBaz); # property setter
}类别::存取器(Moose语法)
package Foo; use Class::Accessor 'antlers'; has bar => (is => 'ro'); # a read-only property has baz => (is => 'rw', isa => 'Bool'); # a read-write property (only 'is' supported, the type is ignored)
Class::Accessor(本机语法)
package Foo; use base qw(Class::Accessor); Foo->mk_accessors(qw(bar baz)); # some read-write properties Foo->mk_accessors(qw(qux)); # a read-only property
类别::细小
package Foo; use Class::Tiny qw(bar baz); # just props
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短