Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# The old way
use v5.10;
use strict;
use warnings;
use Test::More;
use Type::Tiny;
use Types::Standard qw( Int );
my $even = Type::Tiny->new(
name => 'EvenNumber',
parent => Int,
constraint => sub { $_ % 2 == 0 }, ### Coderef
);
ok( $even->check(2) );
ok( $even->check(4) );
ok( not $even->check(3) );
done_testing;
# This works now.
use v5.10;
use strict;
use warnings;
use Test::More;
use Type::Tiny;
use Types::Standard qw( Int );
my $even = Type::Tiny->new(
name => 'EvenNumber',
parent => Int,
constraint => '$_ % 2 == 0', ### String of Perl code
);
ok( $even->check(2) );
ok( $even->check(4) );
ok( not $even->check(3) );
done_testing;
use v5.10;
use strict;
use warnings;
use Test::More;
use Type::Tiny;
use Types::Standard qw( Int );
my $even = Int->where('$_ % 2 == 0'); ### Very concise
ok( $even->check(2) );
ok( $even->check(4) );
ok( not $even->check(3) );
done_testing;
# peek at how the check has been implemented
diag( $even->inline_check('$number') );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment