http://qs321.pair.com?node_id=902332


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

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.

Replies are listed 'Best First'.
Re^5: Moose: What about "class" attributes?
by stvn (Monsignor) on May 02, 2011 at 00:56 UTC

    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?

        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.

        It is largely about scope management and making sure you control how many refs a given object might have. Here is some example (NOTE: I have not run this to confirm it works exactly as expected, but you should get the idea of you are familar with MooseX::Role::Parameterized)

        package My::Parameterized::Role; use MooseX::Role::Parameterized; parameter 'something' => ( isa => 'Str' ); role { my $p = shift; { my $something = $p->param('something'); method 'static_something' => sub { return $something; }; } }
        So, first let me say that this is likely overkill, but having had some nasty ref-counting leaks in the past I have learned to be cautious. So as you can see here, the 'static_something' method I am creating only closes over the $something variable, which we know is a string. If I had written 'static_something' like so:
        method 'static_something' => sub { return $p->param('something'); };
        Then the method would close over the $p object, which *might* become a source of leaks.

        Like I said, probably overkill, but I prefer to be cautious in this kind of environment.

        -stvn