It's not clear what you are asking, but the comments make me think the goal is to avoid returning expensive tied variables when not necessary.

This is what substr, keys, vec and $#a do. The catch is these are ops, so they can simply do

I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;

For a sub, you'd need to locate the op that called the sub first. Contextual::Return can do this for you.

sub foo :lvalue { LVALUE { ... } RVALUE { ... } }

But does this actually speed things up? That's for you to find out.


By the way, there are lighter types of magical variables than tied variables that can be used here.

use Variable::Magic qw( wizard cast ); { my $wiz = wizard( # Called before reading the magic scalar (${$_[0]}). # Assign the value to ${$_[0]} that you want to be fetched. # $data is $_[1]. get => sub { ... }, # Called after an assignment to the magic scalar (${$_[0]}). # $data is $_[1]. set => sub { ... }, ); sub foo :lvalue { LVALUE { my $data = ...; cast(my $magical, $wiz, $data); $magical } RVALUE { ... } } }

In reply to Re: can sub check context for lvalue vs rvalue context? by ikegami
in thread can sub check context for lvalue vs rvalue context? by perl-diddler

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.