Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Re: Re: has it been blessed?

by chipmunk (Parson)
on Mar 07, 2002 at 02:10 UTC ( #149913=note: print w/ replies, xml ) Need Help??

in reply to Re: Re: has it been blessed?
in thread has it been blessed?

Efficiency is nice, but there's something to be said for correctness as well. You've also forgotten REF, CODE, and LVALUE.

Comparing the return value from ref to a list of known types is simply the wrong approach. Either you'll forget something, or a new type will be added in a future version and your code will break.

Go with the UNIVERSAL::isa() approach. Really.

Comment on Re: Re: Re: has it been blessed?
Re: Re: Re: Re: has it been blessed?
by rjray (Chaplain) on Mar 07, 2002 at 20:35 UTC

    I agree, but to clarify, the best approach would seem to be:

    if (ref($obj) and UNIVERSAL::isa($obj, 'UNIVERSAL')) { ... }

    Well, "best" in absence of the blessed primitive, of course...


      if (UNIVERSAL::isa($obj, 'UNIVERSAL')) { is more than enough. If it's not a reference, UNIVERSAL::isa() will return undef. Don't bother doing a check twice.

      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        The reason I wrote it that way is that ref($obj) is a single-opcode operation, much less expensive than the execution of UNIVERSAL::isa($obj, 'UNIVERSAL'). Unless you know for certain that the value being tested will a reference the majority of the time, then you can save a reasonable amount of execution time with ref() coupled with the short-circuiting and operator. I stand behind this one.


        UNIVERSAL::isa($obj, 'UNIVERSAL') will also return true if $obj is the string 'UNIVERSAL', so the ref check is actually useful.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2014-12-22 16:32 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (121 votes), past polls