use Types::Standard qw(Int);
tie(my @numbers, Int);
push @numbers, 1, 2, 3; # ok
push @numbers, "four"; # dies
####
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
##
##
use Type::Tie qw();
use MooseX::Types::Moose qw(Int);
tie(my @numbers, "Type::Tie::ARRAY", Int);
##
##
use Type::Tie qw(ttie);
use MooseX::Types::Moose qw(Int);
ttie(my @numbers, Int);
##
##
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
##
##
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
##
##
use Types::Standard qw(Int);
use Devel::StrictMode qw(STRICT);
my @array_of_ints;
tie @array_of_ints, Int if STRICT;
...; # do stuff here