http://qs321.pair.com?node_id=686835

BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

Perlfunc lists the following possible return values from ref:

SCALAR ARRAY HASH CODE REF GLOB LVALUE

Is that all of them? I've got a vague recollection of either encountering or seeing reference to a 'REGEX' type?

Any others?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re: Missing ref() return possibility?
by toolic (Bishop) on May 16, 2008 at 01:45 UTC
    ref lists these:
    SCALAR ARRAY HASH CODE REF GLOB LVALUE FORMAT IO VSTRING Regexp
Re: Missing ref() return possibility?
by rhesa (Vicar) on May 16, 2008 at 01:42 UTC
    The types you list are the basics. All other cases are covered by "If the referenced object has been blessed into a package, then that package name is returned instead."

    For regular expressions, this is demonstrated by:

    use Scalar::Util qw/blessed/; $q=qr//; print ref($q); print blessed($q); __END__ Regexp Regexp

      What your code implies but isn't quite explicit about is that qr() actually gives back a blessed reference to a scalar.

      use Scalar::Util qw/blessed reftype/; my $q = qr//; print 'ref: ', ref($q), "\n"; print 'blessed: ', blessed($q), "\n"; print 'reftype: ', reftype($q), "\n"; __END__ ref: Regexp blessed: Regexp reftype: SCALAR

      There's some more about this in How can I tell if an object is based on a regex?

Re: Missing ref() return possibility?
by Fletch (Bishop) on May 16, 2008 at 01:47 UTC

    qr// will return an instance that ref returns Regexp for; perhaps that's what you're remembering?

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Missing ref() return possibility?
by runrig (Abbot) on May 16, 2008 at 01:44 UTC
    Did you try perl -e 'print ref(qr/abc/)' ??
Re: Missing ref() return possibility?
by graff (Chancellor) on May 16, 2008 at 05:36 UTC
    It's good to remember that when ref() is given something that is not a reference to anything, it returns an empty string. (It never seems to return "undef", even when you give it nothing at all.)