Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (4)
    As of 2021-03-08 19:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favorite kind of desktop background is:











      Results (127 votes). Check out past polls.

      Notices?