in reply to Re^3: How to differentiate hash reference and object reference?
in thread How to differentiate hash reference and object reference?

Not sure I agree if this is in the context of argument checking -- in which case it should be used with croak (from Carp) and not die.

If the OP is writing a subroutine that takes an argument and then uses that argument to call a method defined for objects of type 'foo', then not checking the parameter means potentially dying with an error message pointing to the line in the OP's subroutine, not pointing to where the subroutine was called with an invalid argument:

sub wibble { my ($self, $obj) = @_; croak "Argument to wibble must be a 'foo' object" if ! ( blessed $obj and $obj->isa('foo') ); if ( $obj->wobble() eq 'spoon' ) { # handle spoon } }

Or, as diotalevi said, use Params::Validate.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.