开发者

How can I declare/use static members in Moose?

开发者 https://www.devze.com 2023-02-14 04:22 出处:网络
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.

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

精彩评论

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