BrowserUk has recently written this article which brought to my knowledge the most interesting thing I've read since I'm at the Monastery. That is, that you can keep (multiple) lvalue refs of substr return values and manipulate them.

Just to be sure, I tried

$ perl -le 'print ref \substr "foo", 1' LVALUE
and so... wow! it seems that we really have a LVALUE datatype. Now, that's cool; OTOH we have lvalue-able subs, but what about them?
$ perl -le 'my $u; sub u :lvalue {$u} print ref \u' SCALAR
Huh, no, it's just a plain old SCALAR. Which raises this meditation: lvalue subs can be cool, but can I imitate substr's action at distance behaviour to do something potentially more interesting? Well, it turns out that I can:
#!/usr/bin/perl -l use strict; use warnings; package Append; sub TIESCALAR { bless \$_[1], $_[0] } sub FETCH { ${${ $_[0] }} } sub STORE { ${${ $_[0] }} .= $_[1] } package main; sub append :lvalue { tie my $v, 'Append', \$_[0]; $v; } my $u='Foo'; (append $u)='Bar'; print $u; __END__
Incidentally, if I substitute
(append $u)='Bar'; print $u;
with
print +(append 'Foo')='Bar';
it still works, whereas I would have expected it to raise an error about an attempt to modify a read-only value. Why doesn't it?
Whatever, this is at best tricky. And this is an oversimpliflied example. It could get too complex in a more serious attempt.

So getting back to the lvref topic hinted to at the beginning of this post, the matter is: the above could be doable in an easier way if

  1. lvalue subs were so as to return a blessed reference into the LVALUE package,
  2. this package provided a, say, STORE method to override standard assignment,
so that the above may be simply:
sub append :lvalue { local *LVALUE::STORE = sub { my $self=shift; $$self .= $_[0]; } $_[0]; }
What about this idea? (Or some variation thereof!)

In reply to lvalues and action at distance by blazar

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.