Path::Class::File or ::Dir & Moose initialiazation and coercion

本秂侑毒 提交于 2019-12-04 09:32:53
coerce 'Path::Class::Entity',  # Base class of ::Dir and ::File
   from Str,
      via {
         if (-d $_) {
            Path::Class::Dir->new($_)
         } else {
            Path::Class::File->new($_)
         }
      };

has 'path' => (
    is       => 'rw',
    lazy     => 1,
    isa      => 'Path::Class::Entity',
    required => 1,
    coerce   => 1,
);

The above assumes the path is a path to a file if path doesn't exist or if another error occurs.

Dave Rolsky

You need to declare the Path::Class::File and PCD types before you use it in a union (I think). Try adding a class_type('Path::Class::File') before this.

Or better yet, use MooseX::Types::Path::Class!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!