I wonder if there is an "official" way to retrieve the order in which attributes have been declared in a Moose-based class

No, there is no official way, Moose stores the attributes in a hash, so they are essentially unordered.

Depending on your needs, there are a number of ways to go about this. The simplest is to make a custom meta-attribute that can take an "order" field. There is a recipe for custom meta-attributes and meta-attribute traits in the Moose::Cookbook and the Moose::Manual also touches on this topic a little too. If you did this then you could write your code like so:

package Hubba; use Moose; has abba => (metaclass => 'MyOrderedCustomAttribute', is => 'rw', orde +r => 1); has zappa => (metaclass => 'MyOrderedCustomAttribute', is => 'rw', ord +er => 2);
Then you could simply get all the attribute using Hubba->meta->get_all_attributes and sort them by their order field. The obvious drawback here is that you have to specify the ordering yourself. Of course with a little trickery and some class level data in your custom meta-attribute you could probably avoid this.

Another (slightly more complex) way would be to have a custom meta-class that simply wrapped the add_attribute method and kept an ordered list of attributes to be retrieved later. The Moose::Cookbook and Moose::Manual also touch on custom meta-classes as well.

One thing for sure though, you should not reach into private attributes (like $h->meta->{_meta_instance}) to get this kind of data, because they are private and therefore should not be relied upon for code outside of the core Moose.

You might also want to try asking this on the Moose mailing list (moose@perl.org) or on the Moose IRC channel (#moose@irc.perl.org) as you are certainly not the first person who has wanted to do this and other people may have already working solutions that you can use.

-stvn

In reply to Re: declaration order of attributes in Moose by stvn
in thread declaration order of attributes in Moose by morgon

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.