.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Type::Tie 3" .TH Type::Tie 3 2024-03-13 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Type::Tie \- tie a variable to a type constraint .SH SYNOPSIS .IX Header "SYNOPSIS" Type::Tie is a response to this sort of problem... .PP .Vb 2 \& use strict; \& use warnings; \& \& { \& package Local::Testing; \& 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\->new(numbers => \e@N); \& \& # Everything OK so far... \& \& # Now watch this! \& push @N, "Monkey!"; \& print $object\->dump; \& \& # Houston, we have a problem! .Ve .PP Just declare \f(CW@N\fR like this: .PP .Vb 2 \& use Type::Tie; \& use Types::Standard qw( Num ); \& \& ttie my @N, Num, ( 1, 2, 3, 3.14159 ); .Ve .PP Now any attempt to add a non-numeric value to \f(CW@N\fR will die. .SH DESCRIPTION .IX Header "DESCRIPTION" This module exports a single function: \f(CW\*(C`ttie\*(C'\fR. \f(CW\*(C`ttie\*(C'\fR 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. .PP .Vb 2 \& use Type::Tie; \& use Types::Standard qw( Int Num ); \& \& ttie my $count, Int\->plus_coercions(Num, \*(Aqint $_\*(Aq), 0; \& \& print tied($count)\->type, "\en"; # \*(AqInt\*(Aq \& \& $count++; # ok \& $count = 2; # ok \& $count = 3.14159; # ok, coerced to 3 \& $count = "Monkey!"; # dies .Ve .PP While the examples in documentation (and the test suite) show type constraints from Types::Standard, any type constraint objects supporting the Type::API interfaces should work. This includes: .IP \(bu 4 Moose::Meta::TypeConstraint / MooseX::Types .IP \(bu 4 Mouse::Meta::TypeConstraint / MouseX::Types .IP \(bu 4 Specio .IP \(bu 4 Type::Tiny .PP However, with Type::Tiny, you don't even need to \f(CW\*(C`use Type::Tie\*(C'\fR. .PP .Vb 1 \& use Types::Standard qw( Int Num ); \& \& tie my $count, Int\->plus_coercions(Num, \*(Aqint $_\*(Aq), 0; \& \& print tied($count)\->type, "\en"; # \*(AqInt\*(Aq \& \& $count++; # ok \& $count = 2; # ok \& $count = 3.14159; # ok, coerced to 3 \& $count = "Monkey!"; # dies .Ve .SS "Cloning tied variables" .IX Subsection "Cloning tied variables" If you clone tied variables with \f(CW\*(C`dclone\*(C'\fR 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. .SH BUGS .IX Header "BUGS" Please report any bugs to . .SH "SEE ALSO" .IX Header "SEE ALSO" Type::API, Type::Tiny, Type::Utils, Moose::Manual::Types, MooseX::Lexical::Types. .SH AUTHOR .IX Header "AUTHOR" Toby Inkster . .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is copyright (c) 2013\-2014, 2018\-2019, 2022\-2023 by Toby Inkster. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .SH "DISCLAIMER OF WARRANTIES" .IX Header "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.