Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Moose "immutabilize" Best Practice

by kcott (Archbishop)
on Dec 20, 2020 at 23:40 UTC ( [id://11125515]=perlquestion: print w/replies, xml ) Need Help??

kcott has asked for the wisdom of the Perl Monks concerning the following question:

Moose::Manual::BestPractices shows the following example in the "namespace::autoclean and immutabilize" section:

package Person; use Moose; use namespace::autoclean; # extends, roles, attributes, etc. # methods __PACKAGE__->meta->make_immutable; 1;

Some discussion follows which includes: "This is preferred to placing no Moose at the end of your package".

I'm currently working through some rather old code (maybe 8-10 years old) which I haven't used in about 5 years. One module was working fine and still does — it didn't need much attention — and I mainly just tidied up the documentation and added some tests. After testing, I installed the new version and thought that was it.

Today, while working on another module of similar vintage, I happened upon the Moose::Manual::BestPractices document. What I read didn't seem to gel with what I remembered from the code I'd put to bed a day or two ago. It looks more like this:

package Person; use Moose; use X; use Y; extends 'Z'; use namespace::autoclean; # roles, attributes, etc. # methods no Z; no Y; no X; no Moose; __PACKAGE__->meta->make_immutable; 1;

I'm reasonably certain I probably would have followed some sort of best practice advice when I wrote that code; although, I really don't remember where that advice might have come from. I'm also aware that best practices change over time: perhaps what was good for Moose in 2010 is less good in 2020.

I'm interested in people's thoughts and opinions on this. What goes on behind the scenes with things like make_immutable is something of a black box to me: information such as "... allows Moose to speed up a lot of things ..." doesn't really help much. I'd like to find out more about this. Pointers to other documentation (instead of writing lengthy posts) are more than welcome.

Thanks in advance.

— Ken

Replies are listed 'Best First'.
Re: Moose "immutabilize" Best Practice
by Haarg (Priest) on Dec 21, 2020 at 09:31 UTC

    The main thing make_immutable does is generate an optimized constructor for your class. You have to do this manually, because Moose doesn't know when you are done defining the class. Rather than going through all of the class metadata during construction, the optimized constructor is built to handle the specific attributes defined in your class.

    namespace::autoclean and no Moose; are doing similar things. They remove imported functions in your class so that they can't be used as methods. This includes things like extends and has. But namespace::autoclean will automatically clean all of the imported functions, so it is much easier to use. Rather than no X; no Y;, a single use namespace::autoclean; will take care of all of the imports. It will also clean functions from modules that don't provide an unimport method to remove functions.

      G'day Haarg,

      ++ Thankyou very much. That was exactly the sort of information I was looking for.

      My understanding of make_immutable, namespace::autoclean and no Moose was very blurry; particularly with respect to how, and to what extent, they interoperated. Your excellent post has cleared that up for me. Thanks again.

      — Ken

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11125515]
Approved by GrandFather
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found