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

Re: Creating accessors on the fly, or not...

by stvn (Monsignor)
on Jun 13, 2009 at 02:19 UTC ( [id://771160]=note: print w/replies, xml ) Need Help??


in reply to Creating accessors on the fly, or not...

he Moose tutorial says you should try to __PACKAGE__->meta->make_immutable; which my current method doesn't allow me to do...

Yes, because make_immutable makes your class readonly so you can't add methods to it (at least not via the MOP).

You can however make a class mutable again with $class->meta->make_mutable and then make it immutable again after that with $class->meta->make_immutable. Of course there is a performance penalty with this, but it's not nearly as bad as you might think.

And lastly, I would suggest replacing

has $accessor => ( is => 'ro' );
with the more MOPish
$self->meta->add_attribute($accessor, (is => 'ro'));

-stvn

Replies are listed 'Best First'.
Re^2: Creating accessors on the fly, or not...
by dsheroh (Monsignor) on Jun 13, 2009 at 12:16 UTC
    Can you point me to a good explanation of why make_immutable is recommended?

    Going through the first 50 google results for "perl moose make_immutable", I came across a few statements that "make_immutable is a Moose best practice", but none of them stated the reason for it. Is this solely because it makes object creation faster (as shown by Pichi's Moose benchmarks) or are there other reasons aside from performance?

      Can you point me to a good explanation of why make_immutable is recommended?

      So in an effort to contribute to the Iron Man blogging challenge I have posted a response here. I will copy the "short answer" part of my post here, but for a more detailed and in-depth explanation check out the blog post.

      So the short answer is that making your class immutable is good because it memoizes several metaclass methods and installs an optimized constructor and destructor for your class and therefore helps reduce a fair amount of the cost (during runtime) of all the abstraction that the MOP provides.

      -stvn
        Thanks for the very thorough answer! It really clarifies why, in Pichi's benchmarks, the immutable version creates objects about 10x faster than the mutable one. (And I'm always happy to provide someone with an excuse for an Iron Man post, seeing as I always seem to be looking for them myself.)
Re^2: Creating accessors on the fly, or not...
by lodin (Hermit) on Jun 14, 2009 at 07:14 UTC

    And lastly, I would suggest replacing
    has $accessor => ( is => 'ro' );
    with the more MOPish
    $self->meta->add_attribute($accessor, (is => 'ro'));

    I'm curious—is there any actual difference (direct or indirect), or is it a matter of principle?

    lodin

      No, there really is very little difference, they are basically equivalent. I guess is it a matter of principle, the declarative "sugar" is meant to be used during class definition time and the MOP is meant to be used at all other times.

      But, that all said, it might make more difference when we work out how to "compile" Moose classes (though I don't know this for sure) because part of that plan would involve caching the generated metaclass and then possibly turning the sugar keywords into noops (at least that was a path we explored in the early MooseX::Compile prototypes). So I guess if you really want to future proof things, it is best to use the MOP in this context. Hopefully by the end of YAPC::NA next week we will know more, it is one of the key topics on the Moose hackathon agenda.

      -stvn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-12-07 17:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.