Re: Moose Type Constraints violations to warnings?

[email protected] (Ævar Arnfjörð Bjarmason)
Newsgroups perl.moose
Message-ID <[email protected]>
On Mon, Jun 13, 2011 at 18:39, Ævar Arnfjörð Bjarmason <[email protected]> wrote:
> For Mouse this monkeypatching would work:

And this would work for Moose:

    package Foo;
    use Moose;
    {
    package Moose::Meta::Attribute;

    sub verify_against_type_constraint {
        my $self = shift;
        my $val  = shift;

        return 1 if !$self->has_type_constraint;

        my $type_constraint = $self->type_constraint;

        $type_constraint->check($val)
            || Carp::cluck("Attribute ("
                     . $self->name
                     . ") does not pass the type constraint because: "
                     . $type_constraint->get_message($val), data => $val, @_);
    }
    }

    has x => (is => "ro", isa => "Int");
    my $foo = Foo->new(x => "blah");
    use Data::Dumper;
    print Dumper $foo;

And give you:

    $ perl -Ilib /tmp/error.pl
    Subroutine RegexpRef redefined at
lib/Moose/Util/TypeConstraints/OptimizedConstraints.pm line 35.
    Subroutine verify_against_type_constraint redefined at /tmp/error.pl line 6.
    Attribute (x) does not pass the type constraint because:
Validation failed for 'Int' with value
blahdatablahinstanceFoo=HASH(0x1edadb0) at /tmp/error.pl line 14
            Moose::Meta::Attribute::verify_against_type_constraint('Moose::Meta::Attribute=HASH(0x27e0078)',
'blah', 'instance', 'Foo=HASH(0x1edadb0)') called at
lib/Moose/Meta/Attribute.pm line 1125
            Moose::Meta::Attribute::_coerce_and_verify('Moose::Meta::Attribute=HASH(0x27e0078)',
'blah', 'Foo=HASH(0x1edadb0)') called at lib/Moose/Meta/Attribute.pm
line 485
            Moose::Meta::Attribute::initialize_instance_slot('Moose::Meta::Attribute=HASH(0x27e0078)',
'Moose::Meta::Instance=HASH(0x27ad5c0)', 'Foo=HASH(0x1edadb0)',
'HASH(0x1ec8f90)') called at lib/Class/MOP/Class.pm line 575
            Class::MOP::Class::_construct_instance('Moose::Meta::Class=HASH(0x266bab0)',
'HASH(0x1ec8f90)') called at lib/Class/MOP/Class.pm line 548
            Class::MOP::Class::new_object('Moose::Meta::Class=HASH(0x266bab0)',
'HASH(0x1ec8f90)') called at lib/Moose/Meta/Class.pm line 252
            Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0x266bab0)',
'HASH(0x1ec8f90)') called at lib/Moose/Object.pm line 22
            Moose::Object::new('Foo', 'x', 'blah') called at
/tmp/error.pl line 23
    $VAR1 = bless( {
                     'x' => 'blah'
                   }, 'Foo' );
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.