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
which makes it clear (or at least clearer) that the purpose of this reference is purely ceremonial.bless "\$$", 'Foo';
I welcome your thoughts.
the lowliest monk
In reply to On References to the Unnamed by tlm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |