my $can_contain = sub { my ( $self, $object ) = @_; $self->container_type eq $object->container; }; sub insert { my ( $self, $object ) = @_; unless ( $self->$can_contain( $object ) ) { die "I can't hold that object!"; } push @{$self}, $object; }