Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Moose: What about "class" attributes?

by stvn (Monsignor)
on May 01, 2011 at 12:39 UTC ( [id://902328]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Moose: What about "class" attributes?
in thread Moose: What about "class" attributes?

So why does MooseX::Traits (which has your name on it) use hacks to achieve exactly that effect?

This is the fun part about Open Source, just cause my name is on it doesn't mean I have done anything other then contribute some part of it and I am no way responsible for the contents ;)

The configuration of trait_namespace will inherit. If you don't like that, why not just use a simple package variable in the package of the object's original created type?

Well, in this case, inheritability is nice, but the goal here is more configurability of subclasses without altering the core MooseX::Traits itself (which would have global effects, which are bad bad bad).

I suppose someone else maintained it and added that bit... but how would you (or how did you originally) do it, in keeping with your stated design philosophy?

I am pretty sure jrockway wrote that part, it is not how I would approach it but this solution is not all that bad (I have seen worse). Keep in mind too that was also written quite a while ago, today one would accomplish this type of thing by making MooseX::Traits into a parameterized role using MooseX::Role::Parameterized.

-stvn

Replies are listed 'Best First'.
Re^4: Moose: What about "class" attributes?
by John M. Dlugosz (Monsignor) on May 01, 2011 at 12:59 UTC
    Ah, so instead of
    with 'MooseX::Traits'; has '+_trait_namespace' => ( default => 'Another' );
    you would write
    with 'MooseX::Traits' => { 'trait_namespace' => 'Another' };
    That could store the string in lexical variable that the instantiated role's code blocks are closed over, so it knows the value you told it. ((I'm borrowing the terminology from C++ templates: instantiate means to spit out a real type from a template))

    That certainly is more to think about.

      Yes, pretty much exactly, except for one thing.

      That could store the string in lexical variable that the instantiated role's code blocks are closed over, so it knows the value you told it.

      Usually for stuff like this, I tend to prefer to make a static method to access the closed over data. Then any of my code uses the static method. It is still basically the same thing, but I prefer to not rely on closed over variables in the role block directly. (It also allows me to be really careful about my reference handling so as to avoid leaks, etc).

      You may find as you delve into the world of MooseX:: that there are some plugins written in the early days of Moose which don't always take advantage of the current set of technologies. MooseX::Storage is a great example of this, it uses a hackish exported "Storage" function that will combine roles for you. If I were to write this today, I would use parameterized roles, but alas that module pre-dated MooseX::Role::Parameterized by several years and to convert it now would be a back-compat nightmare. And in the end, it still works perfectly fine so in this case, why fix something that ain't broke.

      -stvn
        I don't follow you on the advantage or reason for only using the closed-over variable in one place, that being a function that returns it.

        By "static method" you mean a sub that doesn't refer to any instance data (and would see the class name as the first parameter)? Is there a special technique for declaring those in a parameterized role or elsewhere? I did notice that using MooseX::Method::Signatures it doesn't like that as the first argument is checked against Object. But a regular Perl sub inside a code block (such as the role block) doesn't work right, so you must have some other way to declare that, right?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://902328]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-28 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found