开发者

How can I declare/use static members in Moose?

I am usi开发者_如何学Pythonng Moose objects, but I need to declare static members for things that will be done just once and are not object related.

Do you have any ideas or examples?

Thanks

Dotan.


You can use MooseX::ClassAttribute:

package SomeClass;
use Moose;
use MooseX::ClassAttribute;

class_has 'static_member' => ( is => 'rw' );

The member is accesses using SomeClass->static_member.


I tried playing around with MooseX::ClassAttribute as bvr suggested, but I ended up just setting them as read-only members with a default:

has 'static_thing' => ( is => 'ro', init_arg => undef, default => 42 );

It seems simpler.


under all the cervine-ness there is still Plain Old Perl

so just set a variable in the class .pm file

package SomeClass;
use Moose;

my $instance_counter = 0;

around BUILDARGS => sub {
    $instance_counter += 1;
}

. . .
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜