On 1 May 2009, at 16:07, Alex Povolotsky wrote:

Hello!

I'm writing a webmail system, and I have to make a class accessing EMail message. It can be multipart, so bodypart must be an array; it should be lazy, to prevent fetching of data we're not going to display.

Unfortunately, manual for MooseX::AttributeHelpers is rather sparse.

Maybe someone can provide me with an example of lazy array attribute with Moose?


Apart from the mention of it having sparse manual, nothing in your original post suggests you need MXAH, however as requested here is a rather contrived example...

package Message;
use Moose;
use MooseX::AttributeHelpers;

has 'bodyparts' => (
        is => 'ro', lazy_build => 1, metaclass => 'Collection::Array',
        provides => {
                map  => 'each_bodypart',
                grep => 'filter_bodypart',
                get => 'current_bodypart',
                shift => 'next_bodypart',
        },
);

sub _build_bodyparts {
        my $self = shift;
        my $bodyparts = [];
        # ... fetch your bodyparts and stuff them into the arrayref
        return $bodyparts;
 }

HTH

/joel

Reply via email to