There are, it seems, only a few nodes which speak of lvalue subroutines, such as this, which is surprising since they can be quite useful.

The question is, though, how do you know what context the lvalue subroutine is being called in? Here's a simple example:
my $foo; my $foo_new; sub fooz : lvalue { $foo; } $foo = "Bar"; # Initialization print fooz,"\n"; # Reading fooz = "Foo"; # Assignment print fooz,"\n"; # Reading
Within the subroutine, I'm looking for a mechanism similar to wantarray which can return some information about what side of the assignment the sub is on. Here's what I'd like to be able to do:
my $foo; my $foo_new; sub fooz : lvalue { defined ($foo_new)? $foo_new : (islvalue? $foo_new = $foo : $foo); } $foo = "Bar"; # Initialization print fooz,"\n"; # Reading fooz = "Foo"; # Assignment print fooz,"\n"; # Reading
Here the idea is that any writes go into $foo_new, while the reads come from $foo, unless of course there is a $foo_new, in which case they will read from $foo_new.

This all hinges on their being some sort of hint about what side the sub is appearing on, left or right.

Admittely, this is oversimplified, and in this example there will be issues if one decides to assign 'undef' to fooz(). The code is merely for demonstration purposes.

If there is no mechanism presently, is this kind of thing worth implementing as a patch?

In reply to Side Context in an 'lvalue' Subroutine by tadman

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.