use Tie::Constrained;
tie my $array_ref,
'Tie::Constrained', sub { ref $_[0] eq 'ARRAY' };
After that, an attempt to assign anything but an unblessed array ref will fail. Continuing,
tie my $hash_ref,
'Tie::Constrained', sub { ref $_[0] eq 'HASH' };
tie my $scalar_ref,
'Tie::Constrained', sub { ref $_[0] eq 'SCALAR' };
tie my $scalar_value,
'Tie::Constrained', sub { not ref $_[0] };
tie my $query,
'Tie::Constrained',
sub { ref $_[0] and $_[0]->isa('CGI') },
CGI->new;
There are all kinds of possibilities.