Type::Tie(3)

πŸ’– Type::Tie β€” tie a variable to a type constraint πŸ’–

okay bestie so you know how Perl lets you tie a variable to a whole other class that secretly controls it? yeah. this is that, but for type constraints. from the User Contributed Perl Documentation, no less. it's giving "your variable has a bodyguard now". 🫢


✨ SYNOPSIS ✨

so Type::Tie is literally a response to this kind of problem, and the problem is devastating:

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 => \@N);

# Everything OK so far...

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

# Houston, we have a problem!

like 😭 the type check happened ONCE at construction and then @N just... kept living its life. the "Monkey!" snuck in. the constraint never saw it coming. it's giving betrayal.

just declare @N like THIS instead, bestie:

use Type::Tie;
use Types::Standard qw( Num );

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

now any attempt to add a non-numeric value to @N will die. πŸ’€ the "Monkey!" never even gets in the door. no cap.


🌸 DESCRIPTION 🌸

this module exports exactly ONE function: ttie. that's it. that's the whole export list. iconic behaviour honestly. ✨

ttie ties a variable to a type constraint, ensuring that whatever values stored in the variable will conform to the type constraint. and if the type constraint has coercions? those get used if necessary to ensure values assigned to the variable conform. so it's not just a bouncer, it's a bouncer with a glow-up station out front. 🧚

use Type::Tie;
use Types::Standard 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

see how 3.14159 got coerced into 3? the coercion pipeline said "close enough bestie, I got you". and then "Monkey!" said "what about me" and the answer was no. 🚫

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. and that includes:

  • Moose::Meta::TypeConstraint / MooseX::Types
  • Mouse::Meta::TypeConstraint / MouseX::Types
  • Specio
  • Type::Tiny
however! with Type::Tiny, you don't even need to use Type::Tie. πŸ’…
use Types::Standard 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

Type::Tiny is out here doing the whole thing by itself. main character energy, fr fr. πŸ‘‘

πŸ¦‹ 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. πŸ™ˆ

translation: Storable::dclone and Clone = besties. everything else = vibe check pending.


πŸ’€ BUGS πŸ’€

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

(that's a URL, I can't hyperlink it for you, you gotta copy-paste it like it's 2009. sorry bestie. πŸŽ€)


🌈 SEE ALSO 🌈

  • Type::API
  • Type::Tiny
  • Type::Utils
  • Moose::Manual::Types
  • MooseX::Lexical::Types

πŸ‘‘ AUTHOR πŸ‘‘

Toby Inkster <tobyink@cpan.org>.


this software is copyright (c) 2013-2014, 2018-2019, 2022-2025 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.

okay that's the whole page, we ate and left no crumbs πŸ“ this was perl v5.42.0, dated 2026-01-17, and it's the Type::Tie(3) manual page. go tie something responsibly, bestie. mwah. πŸ’–βœ¨

πŸ“¦ where this page comes from