Hello Monks,

I've finally been compelled to the monastery gates for the first time by the intricacies of the Moose meta-model. I by no means mean that pejoratively: on the contrary, it all seems wonderfully elegant. Which makes my inability to work this simple example all the more frustrating!

I'm making a module that will dynamically add attributes to a class at run-time based on key/value pairs in a config file.

The various modules on CPAN which sound like they might fit the bill only seem to fill-in values for 'existing' attributes, i.e. those which have been declared in the client class. My intent is really to turn /any/ key in the config file into an attribute in the client class. So it would look something like MooseX::ConfigFromFile, but creating new attributes instead of merely initializing them.*

Now, as my first foray into making a Moose extension, I have a basic test case:

lib/MooseX/Config/Auto.pm
package MooseX::Config::Auto; use Moose (); use Moose::Exporter; use MooseX::Config::Auto::Role::Meta::Class; Moose::Exporter->setup_import_methods( class_metaroles => { class => ['MooseX::Config::Auto::Role::Meta::Class'], } );
and in lib/MooseX/Config/Auto/Role/Meta/Class.pm
package MooseX::Config::Auto::Role::Meta::Class; use Moose::Role; after new_object => sub { print "BOOP!\n"; };

Now, here's my problem: I feel my module's functionality would be better suited to being a Role that's with'd than a regular module that's use'd. Maybe I'm completely off base with this, but as a theoretical exercise*: Is there in fact any way to get the simple behaviour outlined above via a Role?

The problem seems to be getting a hold of the consuming class's metaclass, in order to apply my own metaroles. I've looked at MooseX::Role::Parameterized, and read through the ins and outs of a number of other modules like MooseX::UndefTolerant and MooseX::GlobRef, but haven't managed to work out how to achieve what I'm after.

My only clue came from adding this code:

print "\t", $_->name, "\n" for __PACKAGE__->meta->meta->calculate_all_ +roles;

Using the 'normal' approach outlined above, sure enough this prints 'MooseX::Config::Auto::Role::Meta::Class'. But my various attempts at the Role method give me the error

"Can't locate object method "calculate_all_roles" via package "Class:: +MOP::Class::Immutable::Class::MOP::Class"

It may be that the answer's staring me in the face, but I'm too lost in the woods at this point to see it.

Put me right monks!



* This sounds like a Bad Idea, and it certainly is. The created attributes will lack most Moosey benefits. It's intended to be used strictly in an early, rapid development phase, where it's more convenient to add and delete lines from a config file than 'hard-code' into your class. Yes, yes, perhaps only very marginally more convenient - and there are perfectly good ways to add many attributes at once in Moose. I accept that this may well be Doing It Wrong from the get-go; but I've learnt a lot about Moose in the process, so I hope you can indulge me purely as an academic endeavour, if not otherwise :)


In reply to Can a Moose Role access & modify the metaclass of classes into which it's composed? by doubi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.