develooper Front page | perl.moose | Postings from June 2011

Re: Moose Type Constraints violations to warnings?

Thread Previous | Thread Next
Ævar Arnfjörð Bjarmason
June 13, 2011 09:45
Re: Moose Type Constraints violations to warnings?
Message ID:
On Mon, Jun 13, 2011 at 18:39, Ævar Arnfjörð Bjarmason <> 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;

            || 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/
    Subroutine RegexpRef redefined at
lib/Moose/Util/TypeConstraints/ line 35.
    Subroutine verify_against_type_constraint redefined at /tmp/ line 6.
    Attribute (x) does not pass the type constraint because:
Validation failed for 'Int' with value
blahdatablahinstanceFoo=HASH(0x1edadb0) at /tmp/ line 14
'blah', 'instance', 'Foo=HASH(0x1edadb0)') called at
lib/Moose/Meta/ line 1125
'blah', 'Foo=HASH(0x1edadb0)') called at lib/Moose/Meta/
line 485
'Moose::Meta::Instance=HASH(0x27ad5c0)', 'Foo=HASH(0x1edadb0)',
'HASH(0x1ec8f90)') called at lib/Class/MOP/ line 575
'HASH(0x1ec8f90)') called at lib/Class/MOP/ line 548
'HASH(0x1ec8f90)') called at lib/Moose/Meta/ line 252
'HASH(0x1ec8f90)') called at lib/Moose/ line 22
            Moose::Object::new('Foo', 'x', 'blah') called at
/tmp/ line 23
    $VAR1 = bless( {
                     'x' => 'blah'
                   }, 'Foo' );

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About