Below I've included a snippet of my type constraints module where I
declare my own personal type constraints and coercions.

I'm trying to make it where I can have an accessor of type YAML, where
I can assign it a string representing a file name.  This file name
gets coerced into an InputFile, which gets coerced into a YAML.

This gives me an error like so:
Attribute (file) does not pass the type constraint (YAML) with
'config.yml' at Moose/Meta/Attribute.pm line 244

Is there any way to chain or nest the constraints like I've tried below?

Thanks,
Dan

-------

use Moose::Util::TypeConstraints;
use IO::File;
use YAML;

subtype 'InputFile'
  => as 'Object'
  => where { $_->isa('IO::Handle') };

coerce 'InputFile'
  => from 'Str'
  => via {
    my $io = IO::File->new();
    $io->open($_, 'r') or IOException->throw( error => "Unable to open
file $_: $!" );
    return $io;
  };

subtype 'YAML'
  => as 'Ref';

coerce 'YAML'
  => from 'InputFile'
  => via {
    my $data = do{ local($/); $_->getline };
    YAML::Load($data);
  };

1;

Reply via email to