use Carp qw(croak);
sub instance_only {
ref(my $self = shift) or croak "instance variable needed";
... use $self as the instance ...
}
sub class_only {
ref(my $class = shift) and croak "class name needed";
... use $class as the class ...
}
Here, the ref function returns true for an
instance or false for a class. If the undesired value is returned,
you'll croak, which has the added
advantage of placing the blame on the caller, not on you. The caller
will get an error message like this, giving the line number in their
code where the wrong method was called:
instance variable needed at their_code line 1234
While this seems like a good thing to do all the time, practically no
CPAN or core modules add this extra checking. Maybe
it's only for the ultra-paranoid.
 |  |  |
9.13. Getters That Double as Setters |  | 9.15. Exercise |