Still exploring what can be done with lvalue subs in closures, I came to realize that the trinary operator is also an lvalue. It may be used as a switch for access to different cloistered lexicals. Consider

{ my ($foo,@foo); sub foo () :lvalue { wantarray ? @foo : $foo; } }
foo becomes the accessor for both the scalar and the array. The ordinary rules of context apply:
foo = 'Flintstone'; (foo) = qw/Fred Wilma Pebbles/; # Hmmm, odd bug, the lines following demonstrate what works # print scalar(foo), ', ', $_, $/ for foo; print join ' & ', foo; print ' ', scalar(foo),$/;
More complicated practices are possible. The scalar might be a coderef, to be called on the array.

Something special can happen in void context, by using a defined wantarray ? : ; term in the accessor, though the options are limited by each term's need to be an lvalue. Here a scalar closure is undefined by being called in void context:

{ my $foo; sub foo () :lvalue { defined wantarray ? $foo : undef $foo; } }
This is just exploration, not part of a project. What can you think of to do with it?

After Compline,
Zaxo


In reply to More Lvalue Subs - Double-Barreled Closures by Zaxo

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.