1Type::Tie(3)          User Contributed Perl Documentation         Type::Tie(3)
2
3
4

NAME

6       Type::Tie - tie a variable to a type constraint
7

SYNOPSIS

9       Type::Tie is a response to this sort of problem...
10
11          use strict;
12          use warnings;
13
14          {
15             package Local::Testing;
16             use Moose;
17             has numbers => ( is => "ro", isa => "ArrayRef[Num]" );
18          }
19
20          # Nice list of numbers.
21          my @N = ( 1, 2, 3, 3.14159 );
22
23          # Create an object with a reference to that list.
24          my $object = Local::Testing->new(numbers => \@N);
25
26          # Everything OK so far...
27
28          # Now watch this!
29          push @N, "Monkey!";
30          print $object->dump;
31
32          # Houston, we have a problem!
33
34       Just declare @N like this:
35
36          use Type::Tie;
37          use Types::Standard qw( Num );
38
39          ttie my @N, Num, ( 1, 2, 3, 3.14159 );
40
41       Now any attempt to add a non-numeric value to @N will die.
42

DESCRIPTION

44       This module exports a single function: "ttie". "ttie" ties a variable
45       to a type constraint, ensuring that whatever values stored in the
46       variable will conform to the type constraint. If the type constraint
47       has coercions, these will be used if necessary to ensure values
48       assigned to the variable conform.
49
50          use Type::Tie;
51          use Types::Standard qw( Int Num );
52
53          ttie my $count, Int->plus_coercions(Num, 'int $_'), 0;
54
55          print tied($count)->type, "\n";   # 'Int'
56
57          $count++;            # ok
58          $count = 2;          # ok
59          $count = 3.14159;    # ok, coerced to 3
60          $count = "Monkey!";  # dies
61
62       While the examples in documentation (and the test suite) show type
63       constraints from Types::Standard, any type constraint objects
64       supporting the Type::API interfaces should work. This includes:
65
66       •   Moose::Meta::TypeConstraint / MooseX::Types
67
68       •   Mouse::Meta::TypeConstraint / MouseX::Types
69
70       •   Specio
71
72       •   Type::Tiny
73
74       However, with Type::Tiny, you don't even need to "use Type::Tie".
75
76          use Types::Standard qw( Int Num );
77
78          tie my $count, Int->plus_coercions(Num, 'int $_'), 0;
79
80          print tied($count)->type, "\n";   # 'Int'
81
82          $count++;            # ok
83          $count = 2;          # ok
84          $count = 3.14159;    # ok, coerced to 3
85          $count = "Monkey!";  # dies
86
87   Cloning tied variables
88       If you clone tied variables with "dclone" from Storable, the clone will
89       also be tied. The Clone module is also able to successfully clone tied
90       variables. With other cloning techniques, your level of success may
91       vary.
92

BUGS

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

SEE ALSO

98       Type::API, Type::Tiny, Type::Utils, Moose::Manual::Types,
99       MooseX::Lexical::Types.
100

AUTHOR

102       Toby Inkster <tobyink@cpan.org>.
103
105       This software is copyright (c) 2013-2014, 2018-2019, 2022-2023 by Toby
106       Inkster.
107
108       This is free software; you can redistribute it and/or modify it under
109       the same terms as the Perl 5 programming language system itself.
110

DISCLAIMER OF WARRANTIES

112       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
113       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
114       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
115
116
117
118perl v5.36.0                      2023-04-24                      Type::Tie(3)
Impressum