# Type::Tie - perldoc - [phpMan]

## NAME
    [Type::Tie](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATie/markdown) - tie a variable to a type constraint

## SYNOPSIS
    [Type::Tie](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATie/markdown) is a response to this sort of problem...

       use strict;
       use warnings;

       {
          package [Local::Testing](https://www.chedong.com/phpMan.php/perldoc/Local%3A%3ATesting/markdown);
          use Moose;
          has numbers => ( is => "ro", isa => "ArrayRef[Num]" );
       }

       # Nice list of numbers.
       my @N = ( 1, 2, 3, 3.14159 );

       # Create an object with a reference to that list.
       my $object = [Local::Testing](https://www.chedong.com/phpMan.php/perldoc/Local%3A%3ATesting/markdown)->new(numbers => \@N);

       # Everything OK so far...

       # Now watch this!
       push @N, "Monkey!";
       print $object->dump;

       # Houston, we have a problem!

    Just declare @N like this:

       use [Type::Tie](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATie/markdown);
       use [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) qw( Num );

       ttie my @N, Num, ( 1, 2, 3, 3.14159 );

    Now any attempt to add a non-numeric value to @N will die.

## DESCRIPTION
    This module exports a single function: "ttie". "ttie" ties a variable to a type constraint,
    ensuring that whatever values stored in the variable will conform to the type constraint. If the
    type constraint has coercions, these will be used if necessary to ensure values assigned to the
    variable conform.

       use [Type::Tie](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATie/markdown);
       use [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) qw( Int Num );

       ttie my $count, Int->plus_coercions(Num, 'int $_'), 0;

       print tied($count)->type, "\n";   # 'Int'

       $count++;            # ok
       $count = 2;          # ok
       $count = 3.14159;    # ok, coerced to 3
       $count = "Monkey!";  # dies

    While the examples in documentation (and the test suite) show type constraints from
    [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown), any type constraint objects supporting the [Type::API](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3AAPI/markdown) interfaces should work.
    This includes:

    *   [Moose::Meta::TypeConstraint](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AMeta%3A%3ATypeConstraint/markdown) / [MooseX::Types](https://www.chedong.com/phpMan.php/perldoc/MooseX%3A%3ATypes/markdown)

    *   [Mouse::Meta::TypeConstraint](https://www.chedong.com/phpMan.php/perldoc/Mouse%3A%3AMeta%3A%3ATypeConstraint/markdown) / [MouseX::Types](https://www.chedong.com/phpMan.php/perldoc/MouseX%3A%3ATypes/markdown)

    *   Specio

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

    However, with [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown), you don't even need to "use [Type::Tie](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATie/markdown)".

       use [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) qw( Int Num );

       tie my $count, Int->plus_coercions(Num, 'int $_'), 0;

       print tied($count)->type, "\n";   # 'Int'

       $count++;            # ok
       $count = 2;          # ok
       $count = 3.14159;    # ok, coerced to 3
       $count = "Monkey!";  # dies

### Cloning tied variables
    If you clone tied variables with "dclone" from Storable, the clone will also be tied. The Clone
    module is also able to successfully clone tied variables. With other cloning techniques, your
    level of success may vary.

## BUGS
    Please report any bugs to <<http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny>>.

## SEE ALSO
    [Type::API](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3AAPI/markdown), [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown), [Type::Utils](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3AUtils/markdown), [Moose::Manual::Types](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AManual%3A%3ATypes/markdown), [MooseX::Lexical::Types](https://www.chedong.com/phpMan.php/perldoc/MooseX%3A%3ALexical%3A%3ATypes/markdown).

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

## COPYRIGHT AND LICENCE
    This software is copyright (c) 2013-2014, 2018-2019, 2022-2023 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.

