I don't know if this I bring up deserves the name "meditation", but I can't figure out a better place to post it than here. It's motivated by a recent thread in clpm.

Perl has handy constructs for creating references to anonymous arrays and hashes, but no such facility for scalars. One could argue that \3 is an anonymous reference to a scalar, since the location that it points to is not associated with any "name" (i.e. variable). But \3 is immutable, which makes it qualitatively different from [3] and +{ 3 => undef }.

I am sure there are excellent reasons behind the decision not to include in Perl an easy way to construct refs to a mutable anonymous scalar. But I have an aesthetic need to be able to invoke to the smallest unit of referencehood, or rather, of anonymous-mutable-referencehood. (Yes, I have strange needs; I'm working on it.)

Among the proposals that came up in the aforementioned thread, the one I liked the most was \"$_". Actually, this is my own minor (I believe) modification of the original proposal, which, in the thread's context was

bless \ "$_", 'Foo' for 3;

Perhaps an even better would be \"$$", whose contents are less potential for variation (as befits anything aspiring to the status of the canonical) than those of $_.

Whence this need to invoke the smallest unit of anonymous-mutable-referencehood? One situation is when defining an object that is not supposed to have any state, but just exist and do all its work through methods. Maybe objects of this class only serve to update the state of the class. I find it distasteful to implement such objects as empty array or hash refs, because the semantics of the implementation are not in harmony with the true nature of the objects. More harmonious, it seems to me, would be to bring such an object to life with a line like

bless "\$$", 'Foo';
which makes it clear (or at least clearer) that the purpose of this reference is purely ceremonial.

I welcome your thoughts.

the lowliest monk


In reply to On References to the Unnamed by tlm

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.