Moose's attribute vs simple sub?

Viewed 110

How to decide - what is the recommended way for the next code fragment?

I have a Moose-based module, where some data is a simple HashRef.

It is possible to write - as a Mooseish HashRef, like:

package Some;
has 'data' => (
        isa => 'HashRef',
        builder => '_build_href',
        init_arg => undef,
        lazy => 1,
);

sub _build-href {
        my $href;
        $href = { a=>'a', b=>'b'}; #some code what builds a href
        return $href;
}

vs

sub  data {
        my $href;
        $href = { a=>'a', b=>'b'}; #some code what builds a href
        return $href;
}

What is the difference? I'm asking because when calling:

my $obj = Some->new;
my $href = $obj->data;

In both case I get a correct HashRef. So when is it recommended to use a Moose-ish has construction (which is longer) vs a simple data sub?

PS: probably this question is so simple for an average perl programmer, but please, keep in mind, I'm still only learning perl.

1 Answers
Related