use Object::MultiType;
my $scalar = 'abcd';
my $obj = Object::MultiType->new( scalar => $scalar );
print %{$obj} ? 'hash' : 'empty hash', "\n";
__END__
empty hash
Object::MultiType does not die here, so eval would see it the same as everything else. (I had a much longer test set for this, but it doesn't really add anything to what I put in Re^4: Reference assessment techniques and how they fail. Specifically, eval has the side effect of calling overload methods, but blokhead's test doesn't.)
We seem to be talking about a difference between the interface as presented and the interface as implemented. That is, maybe it says it can be a hash, but then it croaks when you try to treat it as one. I think there's a whole range of non-function between total failure and total invulnerability. One side of that range (total failure) is where eval outperforms less invasive checks. The other advantage it seems to have is future-proofing against someday there being another kind of scalar that that acts as a hash but doesn't match the less invasive checks. Are those two advantages bigger than determining type without possibly calling unknown code?
(In case it's not obvious, the subtext of my original article is that determining type—reliably—should be easier than this.)
Looking at the code for Scalar::Util::reftype, I don't see where it reblesses. Its vulnerability seems to be that it relies on the object not to have a particular instance method (a_sub_not_likely_to_be_here), which it has defined in UNIVERSAL. Elsewhere refaddr blesses into 'Scalar::Util::Fake', apparently to keep from calling an overloaded stringifier. |