in reply to Is "ref $date eq 'ARRAY'" wrong?
First, doesn't exists the object type ARRAY or the package ARRAY, ok?!
ref(), like the name tell, is just for reference type! Soo, if you make ref() you can have returned the reference for the data type, SCALAR, ARRAY, HASH, GLOB, CODE and REF.
What we need to remember is that an object make a reference to a package, soo ref() also return the name of the package, or where this object is blessed. But the name of the package, let's say FOO, is something very different of the data types SCALAR, ARRAY, HASH, GLOB, CODE. Remember, ref() only take care about reference types!
Soo, if some crazy people say to you to not use ref() to see if you have an ARRAY ref, or an normal SCALAR/string, forget! Actually, ref() was created to be used exactly in this case!
About isa, you should use it for objects, and only for objects! UNIVERSAL, as POD say, is the base "class" for all the objects, since all of them "extends" UNIVERSAL, actually, all of them have UNIVERSAL at @ISA by default.
Soo, this code is trying to know if you have an object that extends the package/class ARRAY, or if it has ARRAY in the @ISA tree, as the name say, isa():
It will work for a reference too, but was not made for that! Soo, if some day some other crazy people make the module/class ARRAY, and than build another module that extends it, let's say ARRAY::EvenCrazy, isa($obj_array_crazy , 'ARRAY') will return true too!UNIVERSAL::isa($date, 'ARRAY')
Now about ref() with objects, the only case that you should use it, is when you explicity want an object blessed in the package FOO, soo is rigth to write:
if ( ref($obj) eq 'FOO' ) {...}
Still on isa(). If you want to accept an object reference, and use it's internal type (ARRAY, HASH, CODE, GLOB) as a non blessed reference/data, you can use isa:
But I never saw that and I think that this is very wrong, specially in OO style, since you are accessing/changing it's internal structure/attributes directly, and not by methods!if ( UNIVERSAL::isa($blessed_in_foo , 'HASH') ) { print "Is an HASH object!\n" ; }
Note that be "paranoic" is the worst thing that you can make in your code. You will create a lot of bugs and make your code slow! You have to use what was created for, to be used for each need, this is how we create cool codes. ;-P
Graciliano M. P.
"Creativity is the expression of the liberty".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Is "ref $date eq 'ARRAY'" wrong? What a mess!
by Anonymous Monk on Dec 20, 2003 at 05:23 UTC | |
by stvn (Monsignor) on Dec 20, 2003 at 16:46 UTC | |
by d_i_r_t_y (Monk) on Dec 21, 2003 at 02:17 UTC | |
by stvn (Monsignor) on Dec 21, 2003 at 04:36 UTC | |
by d_i_r_t_y (Monk) on Dec 21, 2003 at 16:23 UTC | |
| |
by gmpassos (Priest) on Dec 20, 2003 at 17:44 UTC | |
by d_i_r_t_y (Monk) on Dec 21, 2003 at 02:42 UTC | |
by d_i_r_t_y (Monk) on Dec 20, 2003 at 05:28 UTC |