Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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:

Hello,
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?

Comment on Class Attributes with Moo ?
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1010446]
Approved by Athanasius
Front-paged by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-09-01 23:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls