Using Moose, I've got a subtype 'AFile' which coerces a String to a Path::Class::File. I'm trying to create a subtype of 'AFile' called 'ExistingFile' where I'm trying to verify that the file exists. When I create an object that has an attribute of type 'ExistingFile', if I pass in a Path::Class::File, everything "seems" to work, but if I pass in a String, things fail. It appears that in the event the attribute is being coerced, the "where" clause in the 'ExistingFile' subtype is being skipped. Here's some code to illustrate what I'm seeing:

#! /usr/bin/env perl use strict; use warnings; use 5.10.0; package Foo; use Moose; use Moose::Util::TypeConstraints; use namespace::autoclean; use Path::Class qw / file /; subtype 'AFile' => as 'Path::Class::File' => message { 'Item is not a Path::Class::File' }; coerce 'AFile' => from 'Str' => via { file($_) }; subtype 'ExistingFile' => as 'AFile' => where { say "In subtype where clause: $_[0]"; -f $_[0] }, message { "$_[0] does not exist" }; has file => ( isa => 'ExistingFile', is => 'ro', required => 1 ); __PACKAGE__->meta->make_immutable; package main; use Path::Class qw / file /; say $0; eval { my $foo = Foo->new(file => file($0)); }; say ":::".$@.":::"; eval { my $foo = Foo->new(file => $0); }; say ":::".$@.":::";

And the output when I run the above:

subtype_madness.pl In subtype where clause: subtype_madness.pl :::::: :::Attribute (file) does not pass the type constraint because: subtype +_madness.pl does not exist at constructor Foo::new (defined at subtyp +e_madness.pl line 29) line 34 Foo::new('Foo', 'file', 'subtype_madness.pl') called at subtype_ma +dness.pl line 36 eval {...} at subtype_madness.pl line 36 :::

While this example has all the code in one place, in my "real world" the 'AFile' subtype is off in a library somewhere.

I could work around this by creating 'ExistingFile' as a subtype of Path::Class::File, but it *seems* like I should be able to subtype 'AFile'. Thoughts?


In reply to Moose: Problems subtyping a coerced subtype by BaldManTom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.