Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I was under the impression that Moose did not have "class attributes", by which I mean a slot that is associated with all instances of a type rather than being per-instance. In C++ this would be a static member. In Smalltalk I think it's called a class variable.

I looked at Moose Manual Attributes to be sure, and it states, "An attribute is a property that every member of a class has."

Why the double-take? Because I see MooseX::Traits adds an attribute to the class called _trait_namespace whose value is used when you write:

my $class = Class->with_traits('Role')->new( foo => 42 );
How can Class->with_traits use a value found in a property of some specific instance of Class when it is not called on any instance? It sure looks like it must be a class variable!

The instructions on overriding the value of _trait_namespace does not show anything different. So what's going on here?

I want to do something similar, and have a value that is used by some class method (like new) and can be changed by a derived class. This looks like the accessor is a virtual class method (something C++ doesn't have).

In Smalltalk, class slots and virtual class methods are simply normal slots and methods on the metaclass instance (the class object itself). So it makes sense that Moose should accommodate such a thing as it uses the same architecture.

Looking at the source of MooseX::Traits, I see a perfectly ordinary has in a role. However, it is 'bare', so there is no getter and nothing else that would make it usable. Eventually, there is a call to

my $namespace = $class->meta->find_attribute_by_name('_trait_namespace +'); ... $namespace->default ...
So, I guess it's a hack. The slot is never used in an instance, and the default is accessed without any instance being populated with it. (Edit: Worse, I see that if the default is a sub ref, it is called with no arguments at all. The default sub is documented as being called as a method on the object. )

The use of such a thing is testimony that "class variables" and "class methods" are indeed wanted!


In reply to Moose: What about "class" attributes? by John M. Dlugosz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-16 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found