# perldoc > Type::Tiny::ConstrainedObject

## NAME
    [Type::Tiny::ConstrainedObject](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AConstrainedObject/markdown) - shared behavour for [Type::Tiny::Class](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AClass/markdown), etc

## STATUS
    This module is considered experiemental.

## DESCRIPTION
### Methods
    The following methods exist for [Type::Tiny::Class](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AClass/markdown), [Type::Tiny::Role](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3ARole/markdown), [Type::Tiny::Duck](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3ADuck/markdown), and any
    type constraints that inherit from "Object" or "Overload" in [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown).

    These methods will also work for [Type::Tiny::Intersection](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AIntersection/markdown) if at least one of the types in the
    intersection provides these methods.

    These methods will also work for [Type::Tiny::Union](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AUnion/markdown) if all of the types in the union provide
    these methods.

    "stringifies_to($constraint)"
        Generates a new child type constraint which checks the object's stringification against a
        constraint. For example:

           my $type  = [Type::Tiny::Class](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AClass/markdown)->new(class => 'URI');
           my $child = $type->stringifies_to( StrMatch[qr/^http:/] );

           $child->assert_valid( URI->new("<http://example.com/>") );

        In the above example, $child is a type constraint that checks objects are blessed into (or
        inherit from) the URI class, and when stringified (e.g. though overloading) the result
        matches the regular expression "qr/^http:/".

        $constraint may be a type constraint, something that can be coerced to a type constraint
        (such as a coderef returning a boolean), a string of Perl code operating on $_, or a
        reference to a regular expression.

        So the following would work:

           my $child = $type->stringifies_to( sub { qr/^http:/ } );
           my $child = $type->stringifies_to(       qr/^http:/   );
           my $child = $type->stringifies_to(       'm/^http:/'  );

           my $child = $type->where('"$_" =~ /^http:/');

    "numifies_to($constraint)"
        The same as "stringifies_to" but checks numification.

        The following might be useful:

           use [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) qw(Int Overload);
           my $IntLike = Int | Overload->numifies_to(Int)

    "with_attribute_values($attr1 => $constraint1, ...)"
        This is best explained with an example:

           use [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) qw(InstanceOf StrMatch);
           use [Types::Common::Numeric](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3ACommon%3A%3ANumeric/markdown) qw(IntRange);

           my $person = InstanceOf['[Local::Human](https://www.chedong.com/phpMan.php/perldoc/Local%3A%3AHuman/markdown)'];
           my $woman  = $person->with_attribute_values(
              gender   => StrMatch[ qr/^F/i  ],
              age      => IntRange[ 18 => () ],
           );

           $woman->assert_valid($alice);

        This assertion will firstly check that $alice is a [Local::Human](https://www.chedong.com/phpMan.php/perldoc/Local%3A%3AHuman/markdown), then check that
        "$alice->gender" starts with an "F", and lastly check that "$alice->age" is an integer at
        least 18.

        Again, constraints can be type constraints, coderefs, strings of Perl code, or regular
        expressions.

        Technically the "attributes" don't need to be Moo/Moose/Mouse attributes, but any methods
        which can be called with no parameters and return a scalar.

## BUGS
    Please report any bugs to <<https://github.com/tobyink/p5-type-tiny/issues>>.

## SEE ALSO
    [Type::Tiny::Manual](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual/markdown).

    [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown).

## AUTHOR
    Toby Inkster <<tobyink@cpan.org>>.

## COPYRIGHT AND LICENCE
    This software is copyright (c) 2019-2021 by Toby Inkster.

    This is free software; you can redistribute it and/or modify it under the same terms as the Perl
    5 programming language system itself.

## DISCLAIMER OF WARRANTIES
    THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
    WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
    PURPOSE.

