Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by John M. Dlugosz (Monsignor)
on May 02, 2011 at 03:59 UTC ( [id://902440]=note: print w/replies, xml ) Need Help??


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

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?

  • Comment on Re^6: Moose: What about "class" attributes?

Replies are listed 'Best First'.
Re^7: Moose: What about "class" attributes?
by stvn (Monsignor) on May 03, 2011 at 01:41 UTC
    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
      So the design of Param Role goes through all the trouble of making the params full-blown Moose Attributes rather than just arguments, and a parameter object rather than a hashref, but then you are careful not to close over either of them but only on plain values extracted from them.

      The docs show it would be my $something= $p->something. I didn't look to closely before, but now I'm not sure... I guess the parameter object $p is a Moose object that "has" the parameters specified, so ->something is the normal attribute getter for a ro attribute.

      Ref-counting leaks would be some kind of circular reference that the system is not tracking correctly. Or for whatever reason, the past existence of such leaks is why you are leery of closing over the complicated linked-up Moose objects involving anonymous classes.

      If I don't have the same penchant for making everything look like an accessor sub, declaring a lexical variable to hold the value returned from the param's accessor would do the same thing. OTOH, with your accessor method, only that one method needs to be a "closure", if Perl cares whether a closure is actually referring to anything that it might be closed over.

        So the design of Param Role goes through all the trouble of making the params full-blown Moose Attributes rather than just arguments, and a parameter object rather than a hashref, but then you are careful not to close over either of them but only on plain values extracted from them.

        No, those are parameters, they just look like Moose attributes, but underneath they are different.

        Ref-counting leaks would be some kind of circular reference that the system is not tracking correctly. Or for whatever reason, the past existence of such leaks is why you are leery of closing over the complicated linked-up Moose objects involving anonymous classes.

        Yes, Perl is pretty good at doing this, but sometimes if you have values passed around a lot inside objects, you can get a reference you didn't intend to keep. And closures tend to be a big source of these since people don't often notice all that it is they are closing over.

        -stvn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-25 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found