![]() |
|
good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
(tye)Re: ref, no, maybe?by tye (Sage) |
on Jan 11, 2001 at 05:00 UTC ( #51042=note: print w/replies, xml ) | Need Help?? |
I say that the only valid use of ref() is doing something like ! ref($r). I think it is just fine to have string manipulating code that refuses to work on a reference (if you really wanted it to do that, then you can stringify the reference before passing it in). So your code would be fine with me if you wrote it either like this: but I'd prefer this: but note that these will die fatally and rather confusingly if given a hash reference. The correct substitute for ref() is UNIVERSAL::isa, as in:
The problem with "HASH" eq ref($r) is that it will fail if $r is a reference to a blessed hash. Why would you want to refuse to do useful hash stuff on something just because it is blessed? I also don't approve of using: as this could break if stringification is overloaded. Another misuse of ref() is the old: I'd probably leave such a test out completely most of the time, but if you want to do that, then isa() is the right tool: The awkward syntax is required because you can't call a method on an unblessed reference (and calling a method on a non-reference tries to treat it like a string containing a package name, which is only what you want here for a class method such as a constructor). By the way, how do you tell if a reference is blessed without using the fragile hack of: ? - tye (but my friends call me "Tye")
In Section
Seekers of Perl Wisdom
|
|