Created
May 5, 2017 10:12
-
-
Save tobyink/747cab91e84eee7fa3f58a7dd8d909db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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