mtroost has asked for the wisdom of the Perl Monks concerning the following question:

When having a reference, how can i detect what the data type is that it holds? Although perl is loosely typed, one still has warning/error messages that such or such variable isn't the array/hash/code you were looking for. Therefore there could be a way under certain circumstances.
my $result = do_me(); #is $result a code referece? -> $result->() #is $result a scalar?

Replies are listed 'Best First'.
Re: detecting code referece
by moritz (Cardinal) on Dec 17, 2007 at 10:01 UTC
      Scalar::Util's reftype is safer because it avoids the case where the object is blessed into the class 'CODE'.

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: detecting code referece
by Gangabass (Vicar) on Dec 17, 2007 at 10:02 UTC
Re: detecting code referece
by ysth (Canon) on Dec 17, 2007 at 16:23 UTC

      Even more surprising, what if $result is a blessed scalar with &{} overloaded?

      In general, in Perl, there's no foolproof way of knowing the operations you can perform on a reference without performing the operation.