Re: Type constraint messages are not distinct
[email protected] (Stevan Little)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <[email protected]> |
Ernesto,
When Moose throws the type error in an attribute accessor, it will
use the message for the type attached to the attribute. In your case
it is failing in a subtype, so the error message it is showing is
wrong. The type checking happens within a pre-compiled type
constraint that, for efficiencies sake, does not use the full type
constraint object, so it is not easy to get access to the specific
message for the specific subtype it fails on. Of course it is not
impossible, just tricky, so if you would like to either submit a
patch or stop by the #moose IRC channel on irc.perl.org and we will
give you a commit bit and you can experiment in a branch.
Unfortunately I don't have the time to do this myself and the current
push is towards writing better docs, and while I agree it is nice to
have for completeness sake, as you pointed out, its not critical to
the functioning of Moose, so therefore a lower priority.
- Stevan
On Apr 7, 2008, at 11:37 AM, Ernesto wrote:
> Hi Stevan,
>
> this is not really a bug report, it's more a demand of a feature to be
> included in the long run, because it might force heavy changes inside
> the handling of type constraint error messages. And it's not essential
> for using Moose.
>
> If defining subtypes as
>
> # / (root) forbidden
> # no umlauts, no special chars
> #
> subtype 'FilePathString'
> => as 'Str'
> => where { $_ =~ m#^(/[a-zA-Z0-9_.-]+)+$#; }
> => message { "FilePath invalid ($_)" };
>
> subtype 'FilePath'
> => as 'FilePathString'
> => where { -e $_; }
> => message { "FilePath must exist ($_)" };
>
> and using them as
>
> package Foo;
> has 'path' =>
> (
> is => 'ro',
> isa => 'FilePath',
> required => 1,
> );
>
> I get as error message always 'FilePath must exist' instead of the
> expected 'FilePath invalid', when I call
>
> Foo->new ( path => '/' );
>
> Do I miss something?
>
> Please have a look at
> http://dienstleistung-kultur.de/moose/05_type_message.t.txt
>
> As said, I can live without special messages, but I love, when things
> are complete ;-)
>
> Ernesto
>
>
> P.S.: Re-reading this mail it comes to my mind, that I'm not shure
> about
> the order of the type constraint processing.
>
> Maybe I'm better off with something like
>
> subtype 'FilePath'
> => as 'Str'
> => where
> {
> return undef unless $_ =~ m#^(/[a-zA-Z0-9_.-]+)+$#;
> return undef unless -e $_;
> return 1;
> };
> (using the default messages)
>
> to be shure, that the wellformness of the path is checked before its
> existence to avoid handing over malicious paths to the OS.
> Comments?
>
>
> P.P.S.: My environment:
> Perl v5.8.8 on Debian 4.0 Etch
> Moose 0.40 with Moose::Object 0.12 (!)
> Class::MOP 0.54
>
>
>