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

I understand that this is probably the main misunderstanding of any part of perl or Moose on my part, but I seem to be unable to return an ArrayRef from the 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 to do it?

+4
source share
1 answer

splitdir returns a list, not arrayref. You can build arrayref from a list using the [] constructor:

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

Source: https://habr.com/ru/post/1390628/


All Articles