Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Class Attributes with Moo ?

by mascip (Pilgrim)
on Dec 27, 2012 at 05:37 UTC ( #1010446=perlquestion: print w/replies, xml ) Need Help??
mascip has asked for the wisdom of the Perl Monks concerning the following question:

with Moose i used MooseX::ClassAttribute to declare class attributes. I am thinking about transitioning to Moo, and am wondering how to best deal with class attributes.

For example, i am sharing a timer between all the objects of a class. I used to write

package MyClass; use Moose; use MooseX::ClassAttribute; class_has 'timer' => ( is => 'ro', isa => 'Benchmark::Timer', default => sub { Benchmark::Timer->new() }, );

Now with Moo i have just used the "good old way" (maybe not that "good"?) :

package MyClass; use Moo; my $timer = Benchmark::Timer->new();
Is there any nicer way to do that?

Replies are listed 'Best First'.
Re: Class Attributes with Moo ?
by tobyink (Abbot) on Dec 27, 2012 at 07:53 UTC

    Yes, this is a MooX module I've been meaning to write for some time. Like you, I've been using package variables, but a proper class attribute would give you delegation, type constraints, triggers, etc.

    I may have a stab at writing something along these lines in the next couple of weeks. I just need to delve into the Method::Generate::* code to see how feasible it might be.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Thank you for your reply :-) I was wondering if i was missing anything.
      In the future if i need to use inheritance, i might put this timer as a simple attribute, and pass it to my objects from the client code. It's giving some non-related job to the client, but it should do the trick.
      I'll keep an eye open, in case you created a new MooX module. Sorry i cannot help : i don't have the skills nor the time to develop them right now.

        OK, MooX::ClassAttribute is now on CPAN. Subclassing Method::Generate::Accessor to generate class attributes was relatively easy. The hard parts were getting class attributes defined in roles to work, and getting Moose inflation going.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      I don't have the time to try it today, but maybe tomorrow : Class::Data::Inheritable might do the trick according to its documentation.
      This could be enough for me right now.

      The annoying bit is that you have to inherit from Class::Data::Inheritable. I'm not sure how annoying that could be though.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1010446]
Approved by Athanasius
Front-paged by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2018-05-26 21:59 GMT
Find Nodes?
    Voting Booth?