use strict; use warnings; use feature qw( say );
use Types::Common qw( Int PositiveInt );
my @array = ( 1, 2, 3, "foo" );
if ( not Int->all( @array ) ) {
say "They're not all integers";
}
if ( PositiveInt->all( @array ) ) {
say "They're all positive integers";
}
# Another way to think about it:
my $NonInteger = ~Int;
if ( $NonInteger->any( @array ) ) {
say "There's at least one non-integer";
}
2条答案
按热度按时间htrmnn0y1#
Type::Tiny提供了一个快速的方法(
all
)来检查列表中的所有元素是否都与特定的类型内容匹配。iaqfqrcu2#
如果快速和肮脏足够好:
然而,这不能很好地处理例如
"1_200_499"
或"0e0"
这样的有效整数(在Perl中)。