I wanted to build an awesome place for people to discuss module specific issues, but I don't have any more time for this, and there are much better places to discuss Perl-related issues. I'd recommend asking your question on Stack Overflow or on Perl Monks.
If you are looking for a Perl tutorial or Perl-related news, I hope these links will serve you well.
Posted on 2007-03-15 11:46:09-07 by ethan
Moose with Module::Refresh; attributes and method modifiers

Hello.

May I first say that I really enjoy using Moose and appreciate the work that went into it. Thanks; you made the Perl object world a happier place (for me, anyway).

I've been building out an MVC webapp framework with Moose in recent weeks. A couple of concerns have come up:

* there appears to be a problem with making Moose-based modules (or perhaps, specifically, Class::MOP-based modules) refreshable (see Module::Refresh for what I'm talking about); you can force a particular module to be reloaded by Perl, but the attributes will break. I'm thinking this has to do with keeping a bunch of metadata about the Moose modules within the Class::MOP package(s). Have you, or do you know of anyone who has, ever looked into this sort of functionality? Making modules dynamically reloadable is a really marvelous thing when working in development environments (it's obviously something one would turn off in a production environment, of course).

* attributes in Moose/Class::MOP: they create methods within the package, and thus it would seem reasonable that said methods would be subject to modification via the various method modifiers; this works fine if one only uses the attribute accessor on an existing instance, but any method modifiers applied to the attribute will be invisible to the constructor, since the original attribute mutator is kept with a subref in the metadata object. I realize that defaults and triggers provide some degree of flexibility with attributes, but ultimately, maximum flexibility is achieved by using an around modifier on the accessor method; more philosophically, if an attribute is accessed/modified through accessor methods, shouldn't those accessor methods be modifiable just like any other method, to maximize the benefits of encapsulating the attribute behind a method?

Anyway, in the latter item, it seems to me that the keeping of the original attribute accessor method reference in the meta object, to be called directly by the constructor, makes for reduced flexibility and introduces some degree of inconsistency in the Moose interface. I presume it could be eliminated by some additional indirection/introspection within the constructor, which has a performance penalty (well worth it, in my opinion, but I'm just one person). Perhaps I'm entirely misinterpreting the situation, of course. :)

So, if you have any thoughts or comments on these topics, I'll be most happy to hear/read them. Thanks again for the great work.

- Ethan
Direct Responses: 4574 | Write a response
Posted on 2007-03-15 15:35:58-07 by stvn in response to 4573
Re: Moose with Module::Refresh; attributes and method modifiers

Ethan,

Thanks for the comments.

Re: Reloading Moose modules and Module::Refresh

I have actually had similar issues with this while using Moose under mod_perl (and Apache::Reload). I will take a closer look into this and see if I can't find the issue. If you have the time to submit a small test, that would be appreciated. You could either send it directly to me (see my CPAN email), to the Moose mailing list (moose-at-perl-dot-org) or join us on irc.perl.org #moose for a more immediate discussion.

Re: Attributes and accessor wrapping

The accessor methods are not actually called within the constructor, the instance is completely constructed by the instance metaclass (see Class::MOP::Instance for more info) and the values are initialized through the attribute metaclasses (see Class::MOP::Attribute::initialize_instance_slot). Using accessors to construct would only work if all your attributes had writeable accessors. You are correct, you would run into heavy introspection with method modifiers which have been attached in subclasses and the constructors trying to determine the calling context, etc. It could be done, but it would slow things down considerably.

I have been pondering a DBIx::Class-style inflate/deflate feature for attributes, which I suspect could be used to solve the problem you are solving with 'around', and anything that didn't solve might be solveable using a custom attribute metaclass, which are not nearly as scary as they sound ;). Again, if you would like to submit some test cases, and we can discuss from there, feel free to email/IRC-nopaste/mailing-list-post away.

Hope this helps.

- Stevan

Direct Responses: Write a response