Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Trouble getting started with Perl OO (ref $f eq 'HASH' is bad)

by demerphq (Chancellor)
on Oct 07, 2004 at 13:23 UTC ( [id://397291]=note: print w/replies, xml ) Need Help??


in reply to Re: Trouble getting started with Perl OO
in thread Trouble getting started with Perl OO

if (ref $_[0] eq 'HASH') {

This seems to a bit of a religious subject, but personally I consider such checks as this to be totally wrong. You probably really want one of the below:

#1 if (eval{ $_[0]->{''}, 1 }) { #2 use Scalar::Util qw(reftype); if (reftype $_[0] eq 'HASH') { #3 if (UNIVERSAL::isa($_[0],'HASH')) {

I only consider the first to be really robust, the second is fairly robust but could do the wrong thing if the object was actually a different type but had hash overloading semantics. The third is passable, but is easily fooled into causing errors, and the version you used suffers from all of the problems mentioned for my examples as well as the flaw that it plain and simply can't be used with blessed hashes which IMO is just bad design.

Anyway, you may not agree with my POV here, but if you haven't thought your position through on this type of thing its probably worth doing so.

---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi

    Flux8


Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://397291]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-25 12:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found