C
C.DeRykus
>> Quoth Sir Robert Burbridge <[email protected]>:
>>
>> > On 12/09/2009 07:52 PM, Ben Morrow wrote:
>>
>> > > Don't ever check for class membership with 'ref'. In fact, most uses of
>> > > 'ref' except as a boolean are bugs. The correct way to check for class
>> > > membership is to call the ->isa method; if you aren't sure something is
>> > > an object, you can use Scalar::Util::blessed or wrap it in an eval {}.
>>
>> > Out of curiosity, why "Don't ever check for class membership with
>> > 'ref'"? Do you mean, "... unless you want to exclude subclassed
>> > objects", or is there some more dire problem with it that I don't see...
>>
>> Consider
>>
>> ref bless [], "HASH";
>> ref bless [], "0";
>>
>> for why one of reftype or blessed from Scalar::Util is better, depending
>> on what you meant.
>> ...
CD> Aren't 'ref' and 'blessed' equally confusing here...am I
CD> missing something?
CD> perl -wle "use Scalar::Util 'blessed';$o=bless [],'HASH';
CD> print blessed $o;print ref $o"
CD> HASH
CD> HASH
but you know what function you called and can interpret the results
accordingly. $o is blessed into 'HASH' and is also a HASH ref. nothing
confusing there to me. actually reftype would be a better call there as
ref is returning the class, not the type.
Um, I was confused about the earlier recommendation not to use
ref as a generic check for class membership. And, IIUC, a 'ref'
might be returning a builtin type (or even in a very weird case
bless [], 'HASH' -> a package that looks like a builtin type);
whereas, Scalar::Util's blessed reports only a package.