When is the return value of the ref builtin "REF"? What does this say about the argument, and why would I care? Is it possible to concoct a variable with this ref value from pure Perl?

What the ref builtin returns "depends on the type of thing the reference is a reference to", says perlfunc. But according to all the discussions I've seen, a reference in Perl is a scalar. I can print ref \"something" to support this.

Presumably, when using the API, I can create an RV and that would return REF. The following is from sv.c:

switch (SvTYPE(sv)) { /* [snip] */ case SVt_RV: /* [snip IV, PV, etc., all falling through] */ case SVt_PVBM: if (SvROK(sv)) s = "REF"; else s = "SCALAR"; break; case SVt_PVLV: s = SvROK(sv) ? "REF" /* tied lvalues should appear to be * scalars for backwards compatitbility */ : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T') ? "SCALAR" : "LVALUE"; break; case SVt_PVAV: s = "ARRAY"; break; case SVt_PVHV: s = "HASH"; break; /* [snip CODE etc.] */

(Spookiness about "backwards compatibility" deliberately left in.)

So where is this REF thing useful? Where do you even get one? How come my pure perl reference above was not an RV (if it were, I wouldn't get SCALAR as a result of the builtin)?


In reply to ref == "REF" by gaal

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.