in reply to How to tell if I have a string, or a ref to a hash

The tools have been provided already, but I wanted to add a comment: You can't do it perfectly unless you disallow certain special cases related to overloading. Perl is designed around of the concept of implicit coercion, so trying to use type-based polymorphism in Perl is inherently flawed.


By the way, your use of a prototype is not only unnecessary, it's wrong. You tell Perl that the sub takes no arguments ("sub xyz()"), but the sub clearly does take arguments ("while(defined($arg = shift))") and you have to tell Perl to ignore the fact that you told it the sub takes no arguments ("&xyz"). You should have

sub xyz { ... } xyz($s); xyz(\%h);

Replies are listed 'Best First'.
Re^2: How to tell if I have a string, or a ref to a hash
by mshaw (Initiate) on Oct 19, 2010 at 18:06 UTC
    Thanks very much, all. It's interesting that not only had I never heard of 'ref', but that my searches through the index of the Camel Book didn't turn it up. I've decided to use Scalar::Util::reftype.

    Thanks also for the note on prototypes.
      The ref function is in the index of the camel book in both the 2nd and 3rd editions. The original pink one is in a box in my garage, so I don't know about that one.