Ow my god! What a confusion that some monks are making here!

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():

UNIVERSAL::isa($date, 'ARRAY')
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!

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:

if ( UNIVERSAL::isa($blessed_in_foo , 'HASH') ) { print "Is an HASH object!\n" ; }
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!

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".


In reply to Re: Is "ref $date eq 'ARRAY'" wrong? What a mess! by gmpassos
in thread Is "ref $date eq 'ARRAY'" wrong? by bronto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.