Search code examples
perlmoose

MooseX::Declare how can I return an ArrayRef from an attribute default method?


I realise this is probably a basic misunderstanding of some part of perl or Moose on my part, but I don't seem to be able to return an ArrayRef from a default method:

has '_directories' => (
    is => 'ro', 
    isa => 'ArrayRef[Str]', 
    lazy => 1, 
    init_arg => undef, 
    default => method {return File::Spec->splitdir($self->relativeDirectory)});

gives:

Attribute (_directories) does not pass the type constraint because: 
Validation failed for 'ArrayRef[Str]' with value 3

How do I sort this out?


Solution

  • splitdir returns list, not arrayref. You can construct arrayref from list using [] constructor:

    default => method {return [ File::Spec->splitdir($self->relativeDirectory) ] },