Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Type::Tiny is probably best known as a way of having Moose-like type constraints in Moo, but it can be used for so much more. This is the third in a series of posts showing other things you can use Type::Tiny for. This article along with part 1 and part 2 can be found on my blog and in the Cool Uses for Perl section of PerlMonks.

This works:

use Types::Standard qw(Int); tie(my @numbers, Int); push @numbers, 1, 2, 3; # ok push @numbers, "four"; # dies

Well, if you try it, you may find it complains about not being able to load Type::Tie.

Type::Tie is an add-on for Type::Tiny distributed separately. It's an optional dependency, so if you want to use this feature, you'll need to make sure it's installed.

Coercions

This tie feature automatically supports coercions.

use Types::Standard qw(Int Num); my $RoundedInt = Int->plus_coercions( Num, 'int $_' ); tie(my @numbers, $RoundedInt); push @numbers, 1, 2, 3; # ok push @numbers, 4.2; # rounded to 4 push @numbers, "five"; # dies

More about Type::Tie

Type::Tie is designed to be pretty independent of Type::Tiny. You can use it with MooseX::Types, Mouse::Types, and Specio, and it also bundles its own nanoscale type constraint library Type::Nano.

use Type::Tie qw(); use MooseX::Types::Moose qw(Int); tie(my @numbers, "Type::Tie::ARRAY", Int);

To save yourself typing "Type::Tie::ARRAY", "Type::Tie::HASH", and "Type::Tie::SCALAR" all the time, Type::Tie offers a convenience function ttie:

use Type::Tie qw(ttie); use MooseX::Types::Moose qw(Int); ttie(my @numbers, Int);

Use in Attributes

Perl has a type checking hole thanks to references:

use v5.16; package Foo { use Moo; use Types::Standard qw(ArrayRef Int); has numbers => ( required => 1, is => 'ro', isa => ArrayRef[Int], ); } my $foo = Foo->new( numbers => [1, 2, 3] ); push @{ $foo->numbers }, "hi"; # this is allowed

The type constraint is only checked in the constructor and in writers/accessors.

Tying the array allows you to perform type checks and coercions on any new elements added to the array. It's a use for trigger that doesn't suck!

use v5.16; package Foo { use Moo; use Types::Standard qw(ArrayRef Int); has numbers => ( required => 1, is => 'ro', isa => ArrayRef[Int], trigger => sub { tie @{$_[1]}, Int }, ); } my $foo = Foo->new( numbers => [1, 2, 3] ); push @{ $foo->numbers }, "hi"; # dies

With a little bit of work (okay, a lot!) it should be possible to even check deeply nested structures.

Performance

While effort has been made to optimize Type::Tie, tied variables are necessarily slower than untied ones.

If you have an array you want to make sure only contains integers, but you don't want to compromise on performance, you could enable the tie only when you run your test suite, and trust that your test suite will be enough to trigger any potential errors.

use Types::Standard qw(Int); use Devel::StrictMode qw(STRICT); my @array_of_ints; tie @array_of_ints, Int if STRICT; ...; # do stuff here

Devel::StrictMode is a module which exports a constant called STRICT which will be true if the PERL_STRICT, EXTENDED_TESTING, RELEASE_TESTING, or AUTHOR_TESTING environment variables is true, and false otherwise.


In reply to Exploring Type::Tiny Part 3: Using Type::Tie by tobyink

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (8)
    As of 2018-12-13 15:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How many stories does it take before you've heard them all?







      Results (62 votes). Check out past polls.

      Notices?