1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
|
package Types::Standard;
use 5.008001;
use strict;
use warnings;
BEGIN {
eval { require re };
if ( $] < 5.010 ) { require Devel::TypeTiny::Perl58Compat }
}
BEGIN {
$Types::Standard::AUTHORITY = 'cpan:TOBYINK';
$Types::Standard::VERSION = '2.002001';
}
$Types::Standard::VERSION =~ tr/_//d;
use Type::Library -base;
our @EXPORT_OK = qw( slurpy );
use Eval::TypeTiny qw( set_subname );
use Scalar::Util qw( blessed looks_like_number );
use Type::Tiny ();
use Types::TypeTiny ();
my $is_class_loaded;
BEGIN {
$is_class_loaded = q{sub {
no strict 'refs';
return !!0 if ref $_[0];
return !!0 if not $_[0];
return !!0 if ref(do { my $tmpstr = $_[0]; \$tmpstr }) ne 'SCALAR';
my $stash = \%{"$_[0]\::"};
return !!1 if exists($stash->{'ISA'}) && *{$stash->{'ISA'}}{ARRAY} && @{$_[0].'::ISA'};
return !!1 if exists($stash->{'VERSION'});
foreach my $globref (values %$stash) {
return !!1
if ref \$globref eq 'GLOB'
? *{$globref}{CODE}
: ref $globref; # const or sub ref
}
return !!0;
}};
*_is_class_loaded =
Type::Tiny::_USE_XS
? \&Type::Tiny::XS::Util::is_class_loaded
: eval $is_class_loaded;
*_HAS_REFUTILXS = eval {
require Ref::Util::XS;
Ref::Util::XS::->VERSION( 0.100 );
1;
}
? sub () { !!1 }
: sub () { !!0 };
} #/ BEGIN
my $add_core_type = sub {
my $meta = shift;
my ( $typedef ) = @_;
my $name = $typedef->{name};
my ( $xsub, $xsubname );
# We want Map and Tuple to be XSified, even if they're not
# really core.
$typedef->{_is_core} = 1
unless $name eq 'Map' || $name eq 'Tuple';
if ( Type::Tiny::_USE_XS
and not( $name eq 'RegexpRef' ) )
{
$xsub = Type::Tiny::XS::get_coderef_for( $name );
$xsubname = Type::Tiny::XS::get_subname_for( $name );
}
elsif ( Type::Tiny::_USE_MOUSE
and not( $name eq 'RegexpRef' or $name eq 'Int' or $name eq 'Object' ) )
{
require Mouse::Util::TypeConstraints;
$xsub = "Mouse::Util::TypeConstraints"->can( $name );
$xsubname = "Mouse::Util::TypeConstraints::$name" if $xsub;
}
if ( Type::Tiny::_USE_XS
and Type::Tiny::XS->VERSION < 0.014
and $name eq 'Bool' )
{
# Broken implementation of Bool
$xsub = $xsubname = undef;
}
if ( Type::Tiny::_USE_XS
and ( Type::Tiny::XS->VERSION < 0.016 or $] < 5.018 )
and $name eq 'Int' )
{
# Broken implementation of Int
$xsub = $xsubname = undef;
}
$typedef->{compiled_type_constraint} = $xsub if $xsub;
my $orig_inlined = $typedef->{inlined};
if (
defined( $xsubname ) and (
# These should be faster than their normal inlined
# equivalents
$name eq 'Str'
or $name eq 'Bool'
or $name eq 'ClassName'
or $name eq 'RegexpRef'
or $name eq 'FileHandle'
)
)
{
$typedef->{inlined} = sub {
$Type::Tiny::AvoidCallbacks ? goto( $orig_inlined ) : "$xsubname\($_[1])";
};
} #/ if ( defined( $xsubname...))
@_ = ( $meta, $typedef );
goto \&Type::Library::add_type;
};
my $maybe_load_modules = sub {
my $code = pop;
if ( $Type::Tiny::AvoidCallbacks ) {
$code = sprintf(
'do { %s %s; %s }',
$Type::Tiny::SafePackage,
join( '; ', map "use $_ ()", @_ ),
$code,
);
}
$code;
};
sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
my $meta = __PACKAGE__->meta;
# Stringable and LazyLoad are optimizations that complicate
# this module somewhat, but they have led to performance
# improvements. If Types::Standard wasn't such a key type
# library, I wouldn't use them. I strongly discourage anybody
# from using them in their own code. If you're looking for
# examples of how to write a type library sanely, you're
# better off looking at the code for Types::Common::Numeric
# and Types::Common::String.
{
sub Stringable (&) {
bless +{ code => $_[0] }, 'Types::Standard::_Stringable';
}
Types::Standard::_Stringable->Type::Tiny::_install_overloads(
q[""] => sub { $_[0]{text} ||= $_[0]{code}->() } );
sub LazyLoad ($$) {
bless \@_, 'Types::Standard::LazyLoad';
}
'Types::Standard::LazyLoad'->Type::Tiny::_install_overloads(
q[&{}] => sub {
my ( $typename, $function ) = @{ $_[0] };
my $type = $meta->get_type( $typename );
my $class = "Types::Standard::$typename";
eval "require $class; 1" or die( $@ );
# Majorly break encapsulation for Type::Tiny :-O
for my $key ( keys %$type ) {
next unless ref( $type->{$key} ) eq 'Types::Standard::LazyLoad';
my $f = $type->{$key}[1];
$type->{$key} = $class->can( "__$f" );
}
my $mm = $type->{my_methods} || {};
for my $key ( keys %$mm ) {
next unless ref( $mm->{$key} ) eq 'Types::Standard::LazyLoad';
my $f = $mm->{$key}[1];
$mm->{$key} = $class->can( "__$f" );
set_subname(
sprintf( "%s::my_%s", $type->qualified_name, $key ),
$mm->{$key},
);
} #/ for my $key ( keys %$mm)
return $class->can( "__$function" );
},
);
}
no warnings;
BEGIN {
*STRICTNUM =
$ENV{PERL_TYPES_STANDARD_STRICTNUM}
? sub() { !!1 }
: sub() { !!0 }
}
my $_any = $meta->$add_core_type(
{
name => "Any",
inlined => sub { "!!1" },
complement_name => 'None',
type_default => sub { return undef; },
}
);
my $_item = $meta->$add_core_type(
{
name => "Item",
inlined => sub { "!!1" },
parent => $_any,
}
);
my $_bool = $meta->$add_core_type(
{
name => "Bool",
parent => $_item,
constraint => sub {
!ref $_ and ( !defined $_ or $_ eq q() or $_ eq '0' or $_ eq '1' );
},
inlined => sub {
"!ref $_[1] and (!defined $_[1] or $_[1] eq q() or $_[1] eq '0' or $_[1] eq '1')";
},
type_default => sub { return !!0; },
}
);
$_bool->coercion->add_type_coercions( $_any, q{!!$_} );
my $_undef = $meta->$add_core_type(
{
name => "Undef",
parent => $_item,
constraint => sub { !defined $_ },
inlined => sub { "!defined($_[1])" },
type_default => sub { return undef; },
}
);
my $_def = $meta->$add_core_type(
{
name => "Defined",
parent => $_item,
constraint => sub { defined $_ },
inlined => sub { "defined($_[1])" },
complementary_type => $_undef,
}
);
# hackish, but eh
Scalar::Util::weaken( $_undef->{complementary_type} ||= $_def );
my $_val = $meta->$add_core_type(
{
name => "Value",
parent => $_def,
constraint => sub { not ref $_ },
inlined => sub { "defined($_[1]) and not ref($_[1])" },
}
);
my $_str = $meta->$add_core_type(
{
name => "Str",
parent => $_val,
constraint => sub {
ref( \$_ ) eq 'SCALAR' or ref( \( my $val = $_ ) ) eq 'SCALAR';
},
inlined => sub {
"defined($_[1]) and do { ref(\\$_[1]) eq 'SCALAR' or ref(\\(my \$val = $_[1])) eq 'SCALAR' }";
},
sorter => sub { $_[0] cmp $_[1] },
type_default => sub { return ''; },
}
);
my $_laxnum = $meta->add_type(
{
name => "LaxNum",
parent => $_str,
constraint => sub { looks_like_number( $_ ) and ref( \$_ ) ne 'GLOB' },
inlined => sub {
$maybe_load_modules->(
qw/ Scalar::Util /,
'Scalar::Util'->VERSION ge '1.18' # RT 132426
? "defined($_[1]) && !ref($_[1]) && Scalar::Util::looks_like_number($_[1])"
: "defined($_[1]) && !ref($_[1]) && Scalar::Util::looks_like_number($_[1]) && ref(\\($_[1])) ne 'GLOB'"
);
},
sorter => sub { $_[0] <=> $_[1] },
type_default => sub { return 0; },
}
);
my $_strictnum = $meta->add_type(
{
name => "StrictNum",
parent => $_str,
constraint => sub {
my $val = $_;
( $val =~ /\A[+-]?[0-9]+\z/ )
|| (
$val =~ /\A(?:[+-]?) #matches optional +- in the beginning
(?=[0-9]|\.[0-9]) #matches previous +- only if there is something like 3 or .3
[0-9]* #matches 0-9 zero or more times
(?:\.[0-9]+)? #matches optional .89 or nothing
(?:[Ee](?:[+-]?[0-9]+))? #matches E1 or e1 or e-1 or e+1 etc
\z/x
);
},
inlined => sub {
'my $val = '
. $_[1] . ';'
. Value()->inline_check( '$val' )
. ' && ( $val =~ /\A[+-]?[0-9]+\z/ || '
. '$val =~ /\A(?:[+-]?) # matches optional +- in the beginning
(?=[0-9]|\.[0-9]) # matches previous +- only if there is something like 3 or .3
[0-9]* # matches 0-9 zero or more times
(?:\.[0-9]+)? # matches optional .89 or nothing
(?:[Ee](?:[+-]?[0-9]+))? # matches E1 or e1 or e-1 or e+1 etc
\z/x ); '
},
sorter => sub { $_[0] <=> $_[1] },
type_default => sub { return 0; },
}
);
my $_num = $meta->add_type(
{
name => "Num",
parent => ( STRICTNUM ? $_strictnum : $_laxnum ),
}
);
$meta->$add_core_type(
{
name => "Int",
parent => $_num,
constraint => sub { /\A-?[0-9]+\z/ },
inlined => sub {
"do { my \$tmp = $_[1]; defined(\$tmp) and !ref(\$tmp) and \$tmp =~ /\\A-?[0-9]+\\z/ }";
},
type_default => sub { return 0; },
}
);
my $_classn = $meta->add_type(
{
name => "ClassName",
parent => $_str,
constraint => \&_is_class_loaded,
inlined => sub {
$Type::Tiny::AvoidCallbacks
? "($is_class_loaded)->(do { my \$tmp = $_[1] })"
: "Types::Standard::_is_class_loaded(do { my \$tmp = $_[1] })";
},
}
);
$meta->add_type(
{
name => "RoleName",
parent => $_classn,
constraint => sub { not $_->can( "new" ) },
inlined => sub {
$Type::Tiny::AvoidCallbacks
? "($is_class_loaded)->(do { my \$tmp = $_[1] }) and not $_[1]\->can('new')"
: "Types::Standard::_is_class_loaded(do { my \$tmp = $_[1] }) and not $_[1]\->can('new')";
},
}
);
my $_ref = $meta->$add_core_type(
{
name => "Ref",
parent => $_def,
constraint => sub { ref $_ },
inlined => sub { "!!ref($_[1])" },
constraint_generator => sub {
return $meta->get_type( 'Ref' ) unless @_;
my $reftype = shift;
$reftype =~
/^(SCALAR|ARRAY|HASH|CODE|REF|GLOB|LVALUE|FORMAT|IO|VSTRING|REGEXP|Regexp)$/i
or _croak(
"Parameter to Ref[`a] expected to be a Perl ref type; got $reftype" );
$reftype = "$reftype";
return sub {
ref( $_[0] ) and Scalar::Util::reftype( $_[0] ) eq $reftype;
}
},
inline_generator => sub {
my $reftype = shift;
return sub {
my $v = $_[1];
$maybe_load_modules->(
qw/ Scalar::Util /,
"ref($v) and Scalar::Util::reftype($v) eq q($reftype)"
);
};
},
deep_explanation => sub {
require B;
my ( $type, $value, $varname ) = @_;
my $param = $type->parameters->[0];
return if $type->check( $value );
my $reftype = Scalar::Util::reftype( $value );
return [
sprintf(
'"%s" constrains reftype(%s) to be equal to %s', $type, $varname,
B::perlstring( $param )
),
sprintf(
'reftype(%s) is %s', $varname,
defined( $reftype ) ? B::perlstring( $reftype ) : "undef"
),
];
},
}
);
$meta->$add_core_type(
{
name => "CodeRef",
parent => $_ref,
constraint => sub { ref $_ eq "CODE" },
inlined => sub {
_HAS_REFUTILXS && !$Type::Tiny::AvoidCallbacks
? "Ref::Util::XS::is_plain_coderef($_[1])"
: "ref($_[1]) eq 'CODE'";
},
type_default => sub { return sub {}; },
}
);
my $_regexp = $meta->$add_core_type(
{
name => "RegexpRef",
parent => $_ref,
constraint => sub {
ref( $_ ) && !!re::is_regexp( $_ ) or blessed( $_ ) && $_->isa( 'Regexp' );
},
inlined => sub {
my $v = $_[1];
$maybe_load_modules->(
qw/ Scalar::Util re /,
"ref($v) && !!re::is_regexp($v) or Scalar::Util::blessed($v) && $v\->isa('Regexp')"
);
},
type_default => sub { return qr//; },
}
);
$meta->$add_core_type(
{
name => "GlobRef",
parent => $_ref,
constraint => sub { ref $_ eq "GLOB" },
inlined => sub {
_HAS_REFUTILXS && !$Type::Tiny::AvoidCallbacks
? "Ref::Util::XS::is_plain_globref($_[1])"
: "ref($_[1]) eq 'GLOB'";
},
}
);
$meta->$add_core_type(
{
name => "FileHandle",
parent => $_ref,
constraint => sub {
( ref( $_ ) && Scalar::Util::openhandle( $_ ) )
or ( blessed( $_ ) && $_->isa( "IO::Handle" ) );
},
inlined => sub {
$maybe_load_modules->(
qw/ Scalar::Util /,
"(ref($_[1]) && Scalar::Util::openhandle($_[1])) "
. "or (Scalar::Util::blessed($_[1]) && $_[1]\->isa(\"IO::Handle\"))"
);
},
}
);
my $_arr = $meta->$add_core_type(
{
name => "ArrayRef",
parent => $_ref,
constraint => sub { ref $_ eq "ARRAY" },
inlined => sub {
_HAS_REFUTILXS && !$Type::Tiny::AvoidCallbacks
? "Ref::Util::XS::is_plain_arrayref($_[1])"
: "ref($_[1]) eq 'ARRAY'";
},
constraint_generator => LazyLoad( ArrayRef => 'constraint_generator' ),
inline_generator => LazyLoad( ArrayRef => 'inline_generator' ),
deep_explanation => LazyLoad( ArrayRef => 'deep_explanation' ),
coercion_generator => LazyLoad( ArrayRef => 'coercion_generator' ),
type_default => sub { return []; },
type_default_generator => sub {
return $Type::Tiny::parameterize_type->type_default if @_ < 2;
return undef;
},
}
);
my $_hash = $meta->$add_core_type(
{
name => "HashRef",
parent => $_ref,
constraint => sub { ref $_ eq "HASH" },
inlined => sub {
_HAS_REFUTILXS && !$Type::Tiny::AvoidCallbacks
? "Ref::Util::XS::is_plain_hashref($_[1])"
: "ref($_[1]) eq 'HASH'";
},
constraint_generator => LazyLoad( HashRef => 'constraint_generator' ),
inline_generator => LazyLoad( HashRef => 'inline_generator' ),
deep_explanation => LazyLoad( HashRef => 'deep_explanation' ),
coercion_generator => LazyLoad( HashRef => 'coercion_generator' ),
type_default => sub { return {}; },
type_default_generator => sub {
return $Type::Tiny::parameterize_type->type_default if @_ < 2;
return undef;
},
my_methods => {
hashref_allows_key => LazyLoad( HashRef => 'hashref_allows_key' ),
hashref_allows_value => LazyLoad( HashRef => 'hashref_allows_value' ),
},
}
);
$meta->$add_core_type(
{
name => "ScalarRef",
parent => $_ref,
constraint => sub { ref $_ eq "SCALAR" or ref $_ eq "REF" },
inlined => sub { "ref($_[1]) eq 'SCALAR' or ref($_[1]) eq 'REF'" },
constraint_generator => LazyLoad( ScalarRef => 'constraint_generator' ),
inline_generator => LazyLoad( ScalarRef => 'inline_generator' ),
deep_explanation => LazyLoad( ScalarRef => 'deep_explanation' ),
coercion_generator => LazyLoad( ScalarRef => 'coercion_generator' ),
type_default => sub { my $x; return \$x; },
}
);
my $_obj = $meta->$add_core_type(
{
name => "Object",
parent => $_ref,
constraint => sub { blessed $_ },
inlined => sub {
_HAS_REFUTILXS && !$Type::Tiny::AvoidCallbacks
? "Ref::Util::XS::is_blessed_ref($_[1])"
: $maybe_load_modules->(
'Scalar::Util',
"Scalar::Util::blessed($_[1])"
);
},
is_object => 1,
}
);
$meta->$add_core_type(
{
name => "Maybe",
parent => $_item,
constraint_generator => sub {
return $meta->get_type( 'Maybe' ) unless @_;
my $param = Types::TypeTiny::to_TypeTiny( shift );
Types::TypeTiny::is_TypeTiny( $param )
or _croak(
"Parameter to Maybe[`a] expected to be a type constraint; got $param" );
my $param_compiled_check = $param->compiled_check;
my @xsub;
if ( Type::Tiny::_USE_XS ) {
my $paramname = Type::Tiny::XS::is_known( $param_compiled_check );
push @xsub, Type::Tiny::XS::get_coderef_for( "Maybe[$paramname]" )
if $paramname;
}
elsif ( Type::Tiny::_USE_MOUSE and $param->_has_xsub ) {
require Mouse::Util::TypeConstraints;
my $maker = "Mouse::Util::TypeConstraints"->can( "_parameterize_Maybe_for" );
push @xsub, $maker->( $param ) if $maker;
}
return (
sub {
my $value = shift;
return !!1 unless defined $value;
return $param->check( $value );
},
@xsub,
);
},
inline_generator => sub {
my $param = shift;
my $param_compiled_check = $param->compiled_check;
my $xsubname;
if ( Type::Tiny::_USE_XS ) {
my $paramname = Type::Tiny::XS::is_known( $param_compiled_check );
$xsubname = Type::Tiny::XS::get_subname_for( "Maybe[$paramname]" );
}
return unless $param->can_be_inlined;
return sub {
my $v = $_[1];
return "$xsubname\($v\)" if $xsubname && !$Type::Tiny::AvoidCallbacks;
my $param_check = $param->inline_check( $v );
"!defined($v) or $param_check";
};
},
deep_explanation => sub {
my ( $type, $value, $varname ) = @_;
my $param = $type->parameters->[0];
return [
sprintf( '%s is defined', Type::Tiny::_dd( $value ) ),
sprintf(
'"%s" constrains the value with "%s" if it is defined', $type, $param
),
@{ $param->validate_explain( $value, $varname ) },
];
},
coercion_generator => sub {
my ( $parent, $child, $param ) = @_;
return unless $param->has_coercion;
return $param->coercion;
},
type_default => sub { return undef; },
type_default_generator => sub {
$_[0]->type_default || $Type::Tiny::parameterize_type->type_default ;
},
}
);
my $_map = $meta->$add_core_type(
{
name => "Map",
parent => $_hash,
constraint_generator => LazyLoad( Map => 'constraint_generator' ),
inline_generator => LazyLoad( Map => 'inline_generator' ),
deep_explanation => LazyLoad( Map => 'deep_explanation' ),
coercion_generator => LazyLoad( Map => 'coercion_generator' ),
my_methods => {
hashref_allows_key => LazyLoad( Map => 'hashref_allows_key' ),
hashref_allows_value => LazyLoad( Map => 'hashref_allows_value' ),
},
type_default_generator => sub {
return $Type::Tiny::parameterize_type->type_default;
},
}
);
my $_Optional = $meta->add_type(
{
name => "Optional",
parent => $_item,
constraint_generator => sub {
return $meta->get_type( 'Optional' ) unless @_;
my $param = Types::TypeTiny::to_TypeTiny( shift );
Types::TypeTiny::is_TypeTiny( $param )
or _croak(
"Parameter to Optional[`a] expected to be a type constraint; got $param" );
sub { $param->check( $_[0] ) }
},
inline_generator => sub {
my $param = shift;
return unless $param->can_be_inlined;
return sub {
my $v = $_[1];
$param->inline_check( $v );
};
},
deep_explanation => sub {
my ( $type, $value, $varname ) = @_;
my $param = $type->parameters->[0];
return [
sprintf( '%s exists', $varname ),
sprintf( '"%s" constrains %s with "%s" if it exists', $type, $varname, $param ),
@{ $param->validate_explain( $value, $varname ) },
];
},
coercion_generator => sub {
my ( $parent, $child, $param ) = @_;
return unless $param->has_coercion;
return $param->coercion;
},
type_default_generator => sub {
return $_[0]->type_default;
},
}
);
my $_slurpy;
$_slurpy = $meta->add_type(
{
name => "Slurpy",
slurpy => 1,
parent => $_item,
constraint_generator => sub {
my $self = $_slurpy;
my $param = @_ ? Types::TypeTiny::to_TypeTiny(shift) : $_any;
Types::TypeTiny::is_TypeTiny( $param )
or _croak(
"Parameter to Slurpy[`a] expected to be a type constraint; got $param" );
return $self->create_child_type(
slurpy => 1,
display_name => $self->name_generator->( $self, $param ),
parameters => [ $param ],
constraint => sub { $param->check( $_[0] ) },
type_default => $param->type_default,
_build_coercion => sub {
my $coercion = shift;
$coercion->add_type_coercions( @{ $param->coercion->type_coercion_map } )
if $param->has_coercion;
$coercion->freeze;
},
$param->can_be_inlined
? ( inlined => sub { $param->inline_check( $_[1] ) } )
: (),
);
},
deep_explanation => sub {
my ( $type, $value, $varname ) = @_;
my $param = $type->parameters->[0];
return [
sprintf( '%s is slurpy', $varname ),
@{ $param->validate_explain( $value, $varname ) },
];
},
my_methods => {
'unslurpy' => sub {
my $self = shift;
$self->{_my_unslurpy} ||= $self->find_parent(
sub { $_->parent->{uniq} == $_slurpy->{uniq} }
)->type_parameter;
},
'slurp_into' => sub {
my $self = shift;
my $parameters = $self->find_parent(
sub { $_->parent->{uniq} == $_slurpy->{uniq} }
)->parameters;
if ( $parameters->[1] ) {
return $parameters->[1];
}
my $constraint = $parameters->[0];
return 'HASH'
if $constraint->is_a_type_of( HashRef() )
or $constraint->is_a_type_of( Map() )
or $constraint->is_a_type_of( Dict() );
return 'ARRAY';
},
},
}
);
sub slurpy {
my $t = shift;
my $s = $_slurpy->of( $t );
$s->{slurpy} ||= 1;
wantarray ? ( $s, @_ ) : $s;
}
$meta->$add_core_type(
{
name => "Tuple",
parent => $_arr,
name_generator => sub {
my ( $s, @a ) = @_;
sprintf( '%s[%s]', $s, join q[,], @a );
},
constraint_generator => LazyLoad( Tuple => 'constraint_generator' ),
inline_generator => LazyLoad( Tuple => 'inline_generator' ),
deep_explanation => LazyLoad( Tuple => 'deep_explanation' ),
coercion_generator => LazyLoad( Tuple => 'coercion_generator' ),
}
);
$meta->add_type(
{
name => "CycleTuple",
parent => $_arr,
name_generator => sub {
my ( $s, @a ) = @_;
sprintf( '%s[%s]', $s, join q[,], @a );
},
constraint_generator => LazyLoad( CycleTuple => 'constraint_generator' ),
inline_generator => LazyLoad( CycleTuple => 'inline_generator' ),
deep_explanation => LazyLoad( CycleTuple => 'deep_explanation' ),
coercion_generator => LazyLoad( CycleTuple => 'coercion_generator' ),
}
);
$meta->add_type(
{
name => "Dict",
parent => $_hash,
name_generator => sub {
my ( $s, @p ) = @_;
my $l = @p
&& Types::TypeTiny::is_TypeTiny( $p[-1] )
&& $p[-1]->is_strictly_a_type_of( Types::Standard::Slurpy() )
? pop(@p)
: undef;
my %a = @p;
sprintf(
'%s[%s%s]', $s,
join( q[,], map sprintf( "%s=>%s", $_, $a{$_} ), sort keys %a ),
$l ? ",$l" : ''
);
},
constraint_generator => LazyLoad( Dict => 'constraint_generator' ),
inline_generator => LazyLoad( Dict => 'inline_generator' ),
deep_explanation => LazyLoad( Dict => 'deep_explanation' ),
coercion_generator => LazyLoad( Dict => 'coercion_generator' ),
my_methods => {
dict_is_slurpy => LazyLoad( Dict => 'dict_is_slurpy' ),
hashref_allows_key => LazyLoad( Dict => 'hashref_allows_key' ),
hashref_allows_value => LazyLoad( Dict => 'hashref_allows_value' ),
},
}
);
$meta->add_type(
{
name => "Overload",
parent => $_obj,
constraint => sub { require overload; overload::Overloaded( $_ ) },
inlined => sub {
$maybe_load_modules->(
qw/ Scalar::Util overload /,
$INC{'overload.pm'}
? "Scalar::Util::blessed($_[1]) and overload::Overloaded($_[1])"
: "Scalar::Util::blessed($_[1]) and do { use overload (); overload::Overloaded($_[1]) }"
);
},
constraint_generator => sub {
return $meta->get_type( 'Overload' ) unless @_;
my @operations = map {
Types::TypeTiny::is_StringLike( $_ )
? "$_"
: _croak( "Parameters to Overload[`a] expected to be a strings; got $_" );
} @_;
require overload;
return sub {
my $value = shift;
for my $op ( @operations ) {
return unless overload::Method( $value, $op );
}
return !!1;
}
},
inline_generator => sub {
my @operations = @_;
return sub {
require overload;
my $v = $_[1];
$maybe_load_modules->(
qw/ Scalar::Util overload /,
join " and ",
"Scalar::Util::blessed($v)",
map "overload::Method($v, q[$_])", @operations
);
};
},
is_object => 1,
}
);
$meta->add_type(
{
name => "StrMatch",
parent => $_str,
constraint_generator => LazyLoad( StrMatch => 'constraint_generator' ),
inline_generator => LazyLoad( StrMatch => 'inline_generator' ),
}
);
$meta->add_type(
{
name => "OptList",
parent => $_arr,
constraint => sub {
for my $inner ( @$_ ) {
return unless ref( $inner ) eq q(ARRAY);
return unless @$inner == 2;
return unless is_Str( $inner->[0] );
}
return !!1;
},
inlined => sub {
my ( $self, $var ) = @_;
my $Str_check = Str()->inline_check( '$inner->[0]' );
my @code = 'do { my $ok = 1; ';
push @code, sprintf( 'for my $inner (@{%s}) { no warnings; ', $var );
push @code,
sprintf(
'($ok=0) && last unless ref($inner) eq q(ARRAY) && @$inner == 2 && (%s); ',
$Str_check
);
push @code, '} ';
push @code, '$ok }';
return ( undef, join( q( ), @code ) );
},
type_default => sub { return [] },
}
);
$meta->add_type(
{
name => "Tied",
parent => $_ref,
constraint => sub {
!!tied(
Scalar::Util::reftype( $_ ) eq 'HASH' ? %{$_}
: Scalar::Util::reftype( $_ ) eq 'ARRAY' ? @{$_}
: Scalar::Util::reftype( $_ ) =~ /^(SCALAR|REF)$/ ? ${$_}
: undef
);
},
inlined => sub {
my ( $self, $var ) = @_;
$maybe_load_modules->(
qw/ Scalar::Util /,
$self->parent->inline_check( $var )
. " and !!tied(Scalar::Util::reftype($var) eq 'HASH' ? \%{$var} : Scalar::Util::reftype($var) eq 'ARRAY' ? \@{$var} : Scalar::Util::reftype($var) =~ /^(SCALAR|REF)\$/ ? \${$var} : undef)"
);
},
name_generator => sub {
my $self = shift;
my $param = Types::TypeTiny::to_TypeTiny( shift );
unless ( Types::TypeTiny::is_TypeTiny( $param ) ) {
Types::TypeTiny::is_StringLike( $param )
or _croak( "Parameter to Tied[`a] expected to be a class name; got $param" );
require B;
return sprintf( "%s[%s]", $self, B::perlstring( $param ) );
}
return sprintf( "%s[%s]", $self, $param );
},
constraint_generator => LazyLoad( Tied => 'constraint_generator' ),
inline_generator => LazyLoad( Tied => 'inline_generator' ),
}
);
$meta->add_type(
{
name => "InstanceOf",
parent => $_obj,
constraint_generator => sub {
return $meta->get_type( 'InstanceOf' ) unless @_;
require Type::Tiny::Class;
my @classes = map {
Types::TypeTiny::is_TypeTiny( $_ )
? $_
: "Type::Tiny::Class"->new(
class => $_,
display_name => sprintf( 'InstanceOf[%s]', B::perlstring( $_ ) )
)
} @_;
return $classes[0] if @classes == 1;
require B;
require Type::Tiny::Union;
return "Type::Tiny::Union"->new(
type_constraints => \@classes,
display_name => sprintf(
'InstanceOf[%s]', join q[,], map B::perlstring( $_->class ), @classes
),
);
},
}
);
$meta->add_type(
{
name => "ConsumerOf",
parent => $_obj,
constraint_generator => sub {
return $meta->get_type( 'ConsumerOf' ) unless @_;
require B;
require Type::Tiny::Role;
my @roles = map {
Types::TypeTiny::is_TypeTiny( $_ )
? $_
: "Type::Tiny::Role"->new(
role => $_,
display_name => sprintf( 'ConsumerOf[%s]', B::perlstring( $_ ) )
)
} @_;
return $roles[0] if @roles == 1;
require Type::Tiny::Intersection;
return "Type::Tiny::Intersection"->new(
type_constraints => \@roles,
display_name => sprintf(
'ConsumerOf[%s]', join q[,], map B::perlstring( $_->role ), @roles
),
);
},
}
);
$meta->add_type(
{
name => "HasMethods",
parent => $_obj,
constraint_generator => sub {
return $meta->get_type( 'HasMethods' ) unless @_;
require B;
require Type::Tiny::Duck;
return "Type::Tiny::Duck"->new(
methods => \@_,
display_name =>
sprintf( 'HasMethods[%s]', join q[,], map B::perlstring( $_ ), @_ ),
);
},
}
);
$meta->add_type(
{
name => "Enum",
parent => $_str,
constraint_generator => sub {
return $meta->get_type( 'Enum' ) unless @_;
my $coercion;
if ( ref( $_[0] ) and ref( $_[0] ) eq 'SCALAR' ) {
$coercion = ${ +shift };
}
elsif ( ref( $_[0] ) && !blessed( $_[0] )
or blessed( $_[0] ) && $_[0]->isa( 'Type::Coercion' ) )
{
$coercion = shift;
}
require B;
require Type::Tiny::Enum;
return "Type::Tiny::Enum"->new(
values => \@_,
display_name => sprintf( 'Enum[%s]', join q[,], map B::perlstring( $_ ), @_ ),
$coercion ? ( coercion => $coercion ) : (),
);
},
type_default => undef,
}
);
$meta->add_coercion(
{
name => "MkOpt",
type_constraint => $meta->get_type( "OptList" ),
type_coercion_map => [
$_arr, q{ Exporter::Tiny::mkopt($_) },
$_hash, q{ Exporter::Tiny::mkopt($_) },
$_undef, q{ [] },
],
}
);
$meta->add_coercion(
{
name => "Join",
type_constraint => $_str,
coercion_generator => sub {
my ( $self, $target, $sep ) = @_;
Types::TypeTiny::is_StringLike( $sep )
or _croak( "Parameter to Join[`a] expected to be a string; got $sep" );
require B;
$sep = B::perlstring( $sep );
return ( ArrayRef(), qq{ join($sep, \@\$_) } );
},
}
);
$meta->add_coercion(
{
name => "Split",
type_constraint => $_arr,
coercion_generator => sub {
my ( $self, $target, $re ) = @_;
ref( $re ) eq q(Regexp)
or _croak(
"Parameter to Split[`a] expected to be a regular expresssion; got $re" );
my $regexp_string = "$re";
$regexp_string =~ s/\\\//\\\\\//g; # toothpicks
return ( Str(), qq{ [split /$regexp_string/, \$_] } );
},
}
);
__PACKAGE__->meta->make_immutable;
1;
__END__
=pod
=for stopwords booleans vstrings typeglobs
=encoding utf-8
=for stopwords datetimes
=head1 NAME
Types::Standard - bundled set of built-in types for Type::Tiny
=head1 SYNOPSIS
use v5.12;
use strict;
use warnings;
package Horse {
use Moo;
use Types::Standard qw( Str Int Enum ArrayRef Object );
use Type::Params qw( compile );
use namespace::autoclean;
has name => (
is => 'ro',
isa => Str,
required => 1,
);
has gender => (
is => 'ro',
isa => Enum[qw( f m )],
);
has age => (
is => 'rw',
isa => Int->where( '$_ >= 0' ),
);
has children => (
is => 'ro',
isa => ArrayRef[Object],
default => sub { return [] },
);
sub add_child {
state $check = signature(
method => Object,
positional => [ Object ],
); # method signature
my ( $self, $child ) = $check->( @_ ); # unpack @_
push @{ $self->children }, $child;
return $self;
}
}
package main;
my $boldruler = Horse->new(
name => "Bold Ruler",
gender => 'm',
age => 16,
);
my $secretariat = Horse->new(
name => "Secretariat",
gender => 'm',
age => 0,
);
$boldruler->add_child( $secretariat );
use Types::Standard qw( is_Object assert_Object );
# is_Object($thing) returns a boolean
my $is_it_an_object = is_Object($boldruler);
# assert_Object($thing) returns $thing or dies
say assert_Object($boldruler)->name; # says "Bold Ruler"
=head1 STATUS
This module is covered by the
L<Type-Tiny stability policy|Type::Tiny::Manual::Policies/"STABILITY">.
=head1 DESCRIPTION
This documents the details of the L<Types::Standard> type library.
L<Type::Tiny::Manual> is a better starting place if you're new.
L<Type::Tiny> bundles a few types which seem to be useful.
=head2 Moose-like
The following types are similar to those described in
L<Moose::Util::TypeConstraints>.
=over
=item *
B<< Any >>
Absolutely any value passes this type constraint (even undef).
=item *
B<< Item >>
Essentially the same as B<Any>. All other type constraints in this library
inherit directly or indirectly from B<Item>.
=item *
B<< Bool >>
Values that are reasonable booleans. Accepts 1, 0, the empty string and
undef.
=item *
B<< Maybe[`a] >>
Given another type constraint, also accepts undef. For example,
B<< Maybe[Int] >> accepts all integers plus undef.
=item *
B<< Undef >>
Only undef passes this type constraint.
=item *
B<< Defined >>
Only undef fails this type constraint.
=item *
B<< Value >>
Any defined, non-reference value.
=item *
B<< Str >>
Any string.
(The only difference between B<Value> and B<Str> is that the former accepts
typeglobs and vstrings.)
Other customers also bought: B<< StringLike >> from L<Types::TypeTiny>.
=item *
B<< Num >>
See B<LaxNum> and B<StrictNum> below.
=item *
B<< Int >>
An integer; that is a string of digits 0 to 9, optionally prefixed with a
hyphen-minus character.
Expect inconsistent results for dualvars, and numbers too high (or negative
numbers too low) for Perl to safely represent as an integer.
=item *
B<< ClassName >>
The name of a loaded package. The package must have C<< @ISA >> or
C<< $VERSION >> defined, or must define at least one sub to be considered
a loaded package.
=item *
B<< RoleName >>
Like B<< ClassName >>, but the package must I<not> define a method called
C<new>. This is subtly different from Moose's type constraint of the same
name; let me know if this causes you any problems. (I can't promise I'll
change anything though.)
=item *
B<< Ref[`a] >>
Any defined reference value, including blessed objects.
Unlike Moose, B<Ref> is a parameterized type, allowing Scalar::Util::reftype
checks, a la
Ref["HASH"] # hashrefs, including blessed hashrefs
=item *
B<< ScalarRef[`a] >>
A value where C<< ref($value) eq "SCALAR" or ref($value) eq "REF" >>.
If parameterized, the referred value must pass the additional constraint.
For example, B<< ScalarRef[Int] >> must be a reference to a scalar which
holds an integer value.
=item *
B<< ArrayRef[`a] >>
A value where C<< ref($value) eq "ARRAY" >>.
If parameterized, the elements of the array must pass the additional
constraint. For example, B<< ArrayRef[Num] >> must be a reference to an
array of numbers.
As an extension to Moose's B<ArrayRef> type, a minimum and maximum array
length can be given:
ArrayRef[CodeRef, 1] # ArrayRef of at least one CodeRef
ArrayRef[FileHandle, 0, 2] # ArrayRef of up to two FileHandles
ArrayRef[Any, 0, 100] # ArrayRef of up to 100 elements
Other customers also bought: B<< ArrayLike >> from L<Types::TypeTiny>.
=item *
B<< HashRef[`a] >>
A value where C<< ref($value) eq "HASH" >>.
If parameterized, the values of the hash must pass the additional
constraint. For example, B<< HashRef[Num] >> must be a reference to an
hash where the values are numbers. The hash keys are not constrained,
but Perl limits them to strings; see B<Map> below if you need to further
constrain the hash values.
Other customers also bought: B<< HashLike >> from L<Types::TypeTiny>.
=item *
B<< CodeRef >>
A value where C<< ref($value) eq "CODE" >>.
Other customers also bought: B<< CodeLike >> from L<Types::TypeTiny>.
=item *
B<< RegexpRef >>
A reference where C<< re::is_regexp($value) >> is true, or
a blessed reference where C<< $value->isa("Regexp") >> is true.
=item *
B<< GlobRef >>
A value where C<< ref($value) eq "GLOB" >>.
=item *
B<< FileHandle >>
A file handle.
=item *
B<< Object >>
A blessed object.
(This also accepts regexp refs.)
=back
=head2 Structured
Okay, so I stole some ideas from L<MooseX::Types::Structured>.
=over
=item *
B<< Map[`k, `v] >>
Similar to B<HashRef> but parameterized with type constraints for both the
key and value. The constraint for keys would typically be a subtype of
B<Str>.
=item *
B<< Tuple[...] >>
Subtype of B<ArrayRef>, accepting a list of type constraints for
each slot in the array.
B<< Tuple[Int, HashRef] >> would match C<< [1, {}] >> but not C<< [{}, 1] >>.
=item *
B<< Dict[...] >>
Subtype of B<HashRef>, accepting a list of type constraints for
each slot in the hash.
For example B<< Dict[name => Str, id => Int] >> allows
C<< { name => "Bob", id => 42 } >>.
=item *
B<< Optional[`a] >>
Used in conjunction with B<Dict> and B<Tuple> to specify slots that are
optional and may be omitted (but not necessarily set to an explicit undef).
B<< Dict[name => Str, id => Optional[Int]] >> allows C<< { name => "Bob" } >>
but not C<< { name => "Bob", id => "BOB" } >>.
Note that any use of B<< Optional[`a] >> outside the context of
parameterized B<Dict> and B<Tuple> type constraints makes little sense,
and its behaviour is undefined. (An exception: it is used by
L<Type::Params> for a similar purpose to how it's used in B<Tuple>.)
=back
This module also exports a B<Slurpy> parameterized type, which can be
used as follows.
It can cause additional trailing values in a B<Tuple> to be slurped
into a structure and validated. For example, slurping into an arrayref:
my $type = Tuple[ Str, Slurpy[ ArrayRef[Int] ] ];
$type->( ["Hello"] ); # ok
$type->( ["Hello", 1, 2, 3] ); # ok
$type->( ["Hello", [1, 2, 3]] ); # not ok
Or into a hashref:
my $type2 = Tuple[ Str, Slurpy[ Map[Int, RegexpRef] ] ];
$type2->( ["Hello"] ); # ok
$type2->( ["Hello", 1, qr/one/i, 2, qr/two/] ); # ok
It can cause additional values in a B<Dict> to be slurped into a
hashref and validated:
my $type3 = Dict[ values => ArrayRef, Slurpy[ HashRef[Str] ] ];
$type3->( { values => [] } ); # ok
$type3->( { values => [], name => "Foo" } ); # ok
$type3->( { values => [], name => [] } ); # not ok
In either B<Tuple> or B<Dict>, B<< Slurpy[Any] >> can be used to indicate
that additional values are acceptable, but should not be constrained in
any way.
B<< Slurpy[Any] >> is an optimized code path. Although the following are
essentially equivalent checks, the former should run a lot faster:
Tuple[ Int, Slurpy[Any] ]
Tuple[ Int, Slurpy[ArrayRef] ]
A function C<< slurpy($type) >> is also exported which was historically
how slurpy types were created.
Outside of B<Dict> and B<Tuple>, B<< Slurpy[Foo] >> should just act the
same as B<Foo>. But don't do that.
=begin trustme
=item slurpy
=end trustme
=head2 Objects
Okay, so I stole some ideas from L<MooX::Types::MooseLike::Base>.
=over
=item *
B<< InstanceOf[`a] >>
Shortcut for a union of L<Type::Tiny::Class> constraints.
B<< InstanceOf["Foo", "Bar"] >> allows objects blessed into the C<Foo>
or C<Bar> classes, or subclasses of those.
Given no parameters, just equivalent to B<Object>.
=item *
B<< ConsumerOf[`a] >>
Shortcut for an intersection of L<Type::Tiny::Role> constraints.
B<< ConsumerOf["Foo", "Bar"] >> allows objects where C<< $o->DOES("Foo") >>
and C<< $o->DOES("Bar") >> both return true.
Given no parameters, just equivalent to B<Object>.
=item *
B<< HasMethods[`a] >>
Shortcut for a L<Type::Tiny::Duck> constraint.
B<< HasMethods["foo", "bar"] >> allows objects where C<< $o->can("foo") >>
and C<< $o->can("bar") >> both return true.
Given no parameters, just equivalent to B<Object>.
=back
=head2 More
There are a few other types exported by this module:
=over
=item *
B<< Overload[`a] >>
With no parameters, checks that the value is an overloaded object. Can
be given one or more string parameters, which are specific operations
to check are overloaded. For example, the following checks for objects
which overload addition and subtraction.
Overload["+", "-"]
=item *
B<< Tied[`a] >>
A reference to a tied scalar, array or hash.
Can be parameterized with a type constraint which will be applied to
the object returned by the C<< tied() >> function. As a convenience,
can also be parameterized with a string, which will be inflated to a
L<Type::Tiny::Class>.
use Types::Standard qw(Tied);
use Type::Utils qw(class_type);
my $My_Package = class_type { class => "My::Package" };
tie my %h, "My::Package";
\%h ~~ Tied; # true
\%h ~~ Tied[ $My_Package ]; # true
\%h ~~ Tied["My::Package"]; # true
tie my $s, "Other::Package";
\$s ~~ Tied; # true
$s ~~ Tied; # false !!
If you need to check that something is specifically a reference to
a tied hash, use an intersection:
use Types::Standard qw( Tied HashRef );
my $TiedHash = (Tied) & (HashRef);
tie my %h, "My::Package";
tie my $s, "Other::Package";
\%h ~~ $TiedHash; # true
\$s ~~ $TiedHash; # false
=item *
B<< StrMatch[`a] >>
A string that matches a regular expression:
declare "Distance",
as StrMatch[ qr{^([0-9]+)\s*(mm|cm|m|km)$} ];
You can optionally provide a type constraint for the array of subexpressions:
declare "Distance",
as StrMatch[
qr{^([0-9]+)\s*(.+)$},
Tuple[
Int,
enum(DistanceUnit => [qw/ mm cm m km /]),
],
];
Here's an example using L<Regexp::Common>:
package Local::Host {
use Moose;
use Regexp::Common;
has ip_address => (
is => 'ro',
required => 1,
isa => StrMatch[qr/^$RE{net}{IPv4}$/],
default => '127.0.0.1',
);
}
On certain versions of Perl, type constraints of the forms
B<< StrMatch[qr/../ >> and B<< StrMatch[qr/\A..\z/ >> with any number
of intervening dots can be optimized to simple length checks.
=item *
B<< Enum[`a] >>
As per MooX::Types::MooseLike::Base:
has size => (
is => "ro",
isa => Enum[qw( S M L XL XXL )],
);
You can enable coercion by passing C<< \1 >> before the list of values.
has size => (
is => "ro",
isa => Enum[ \1, qw( S M L XL XXL ) ],
coerce => 1,
);
This will use the C<closest_match> method in L<Type::Tiny::Enum> to
coerce closely matching strings.
=item *
B<< OptList >>
An arrayref of arrayrefs in the style of L<Data::OptList> output.
=item *
B<< LaxNum >>, B<< StrictNum >>
In Moose 2.09, the B<Num> type constraint implementation was changed from
being a wrapper around L<Scalar::Util>'s C<looks_like_number> function to
a stricter regexp (which disallows things like "-Inf" and "Nan").
Types::Standard provides I<both> implementations. B<LaxNum> is measurably
faster.
The B<Num> type constraint is currently an alias for B<LaxNum> unless you
set the C<PERL_TYPES_STANDARD_STRICTNUM> environment variable to true before
loading Types::Standard, in which case it becomes an alias for B<StrictNum>.
The constant C<< Types::Standard::STRICTNUM >> can be used to check if
B<Num> is being strict.
Most people should probably use B<Num> or B<StrictNum>. Don't explicitly
use B<LaxNum> unless you specifically need an attribute which will accept
things like "Inf".
=item *
B<< CycleTuple[`a] >>
Similar to B<Tuple>, but cyclical.
CycleTuple[Int, HashRef]
will allow C<< [1,{}] >> and C<< [1,{},2,{}] >> but disallow
C<< [1,{},2] >> and C<< [1,{},2,[]] >>.
I think you understand B<CycleTuple> already.
Currently B<Optional> and B<Slurpy> parameters are forbidden. There are
fairly limited use cases for them, and it's not exactly clear what they
should mean.
The following is an efficient way of checking for an even-sized arrayref:
CycleTuple[Any, Any]
The following is an arrayref which would be suitable for coercing to a
hashref:
CycleTuple[Str, Any]
All the examples so far have used two parameters, but the following is
also a possible B<CycleTuple>:
CycleTuple[Str, Int, HashRef]
This will be an arrayref where the 0th, 3rd, 6th, etc values are
strings, the 1st, 4th, 7th, etc values are integers, and the 2nd,
5th, 8th, etc values are hashrefs.
=back
=head2 Coercions
Most of the types in this type library have no coercions by default.
The exception is B<Bool> as of Types::Standard 1.003_003, which coerces
from B<Any> via C<< !!$_ >>.
Some standalone coercions may be exported. These can be combined
with type constraints using the C<< plus_coercions >> method.
=over
=item *
B<< MkOpt >>
A coercion from B<ArrayRef>, B<HashRef> or B<Undef> to B<OptList>. Example
usage in a Moose attribute:
use Types::Standard qw( OptList MkOpt );
has options => (
is => "ro",
isa => OptList->plus_coercions( MkOpt ),
coerce => 1,
);
=item *
B<< Split[`a] >>
Split a string on a regexp.
use Types::Standard qw( ArrayRef Str Split );
has name => (
is => "ro",
isa => ArrayRef->of(Str)->plus_coercions(Split[qr/\s/]),
coerce => 1,
);
=item *
B<< Join[`a] >>
Join an array of strings with a delimiter.
use Types::Standard qw( Str Join );
my $FileLines = Str->plus_coercions(Join["\n"]);
has file_contents => (
is => "ro",
isa => $FileLines,
coerce => 1,
);
=back
=head2 Constants
=over
=item C<< Types::Standard::STRICTNUM >>
Indicates whether B<Num> is an alias for B<StrictNum>. (It is usually an
alias for B<LaxNum>.)
=back
=head2 Environment
=over
=item C<PERL_TYPES_STANDARD_STRICTNUM>
Switches to more strict regexp-based number checking instead of using
C<looks_like_number>.
=item C<PERL_TYPE_TINY_XS>
If set to false, can be used to suppress the loading of XS implementions of
some type constraints.
=item C<PERL_ONLY>
If C<PERL_TYPE_TINY_XS> does not exist, can be set to true to suppress XS
usage similarly. (Several other CPAN distributions also pay attention to this
environment variable.)
=back
=begin private
=item Stringable
=item LazyLoad
=end private
=head1 BUGS
Please report any bugs to
L<https://github.com/tobyink/p5-type-tiny/issues>.
=head1 SEE ALSO
L<The Type::Tiny homepage|https://typetiny.toby.ink/>.
L<Type::Tiny::Manual>.
L<Type::Tiny>, L<Type::Library>, L<Type::Utils>, L<Type::Coercion>.
L<Moose::Util::TypeConstraints>,
L<Mouse::Util::TypeConstraints>,
L<MooseX::Types::Structured>.
L<Types::XSD> provides some type constraints based on XML Schema's data
types; this includes constraints for ISO8601-formatted datetimes, integer
ranges (e.g. B<< PositiveInteger[maxInclusive=>10] >> and so on.
L<Types::Encodings> provides B<Bytes> and B<Chars> type constraints that
were formerly found in Types::Standard.
L<Types::Common::Numeric> and L<Types::Common::String> provide replacements
for L<MooseX::Types::Common>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2013-2014, 2017-2023 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.
=head1 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.
|