Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: can sub check context for lvalue vs rvalue context?

by perl-diddler (Chaplain)
on May 09, 2018 at 03:00 UTC ( [id://1214251]=note: print w/replies, xml ) Need Help??


in reply to Re: can sub check context for lvalue vs rvalue context?
in thread can sub check context for lvalue vs rvalue context?

What happens with Tie? Does it call a separate Store then Fetch?

Either way, a new value has to be passed into 'X', so it has to be called first in an lvalue context. Then it depends on whether or not perl makes a separate call to the function to do the equivalent of a fetch operation.

If it does, the 2nd call would call it in rvalue context, but I'd think the 2nd call wouldn't happen, as perl would already have the value it stored and not need to make a 2nd call.

  • Comment on Re^2: can sub check context for lvalue vs rvalue context?

Replies are listed 'Best First'.
Re^3: can sub check context for lvalue vs rvalue context?
by BrowserUk (Patriarch) on May 09, 2018 at 11:23 UTC

    Well, its easy to demonstrate there is only one call made. So what now?

    I think the easiest way to think of it, is that the function returns an lvalue to the calling context; and has no say or knowledge of how that lvalue is used there.

    And in reality, that is exactly what happens:

    { my $X = 12345; sub x :lvalue { $X } };; print x();; 12345 $r = \x();; print x();; 12345 $$r = 456;; print x();; 456

    Think of a function returning a reference. It does not know and cannot influence whether that reference will be used to read the referent value, or assign a new value to it.

    This is analogous; thus even if you knew what the context was, it would not benefit you to know. (Also why :lvalue never made it off the 'experimental' list.)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
      But it does benefit me to know. If it I got that it was called in rvalue context, that would be a problem. But that it is called in lvalue context, would be what I'd need. I'd want to detect that it was called in lvalue context to give it the more expensive tied var. I could return a simple variable if it was called in rvalue context, only, as an optimization -- since reading a var is often more frequently done than setting it.

      The same type of situation already arises with "wantarray":

      $x = do{ my @y = func() };

      You could ask "well does sub get that it was called wanting an array or not?" Even though the assignment to the array is thrown away, it will will be called with wantarray=1; To say dual use subs should have been left as "experimental" because the end result is that only the scalar value is assigned to $x, is obviously not what has been done -- subs can return arrays or scalars based on context.

      The same situation holds with the no-longer-experimental ':lvalue' subs. They just need the same treatment as dual-return subs returning scalar or array -- i.e. a keyword to let the sub know context.

        The same situation holds with the no-longer-experimental ':lvalue' subs. They just need the same treatment as dual-return subs returning scalar or array -- i.e. a keyword to let the sub know context.

        (Part of) My point is that the caller can take a reference to whatever you return, and at some point later dereference that pointer and assign through it; with the expectation that the next time they call your sub, it will return the new value they've assigned; but taking a reference is explicitly an rvalue operation.

        If you could detect the rvalue context and returned "a simple rvalue", you break that expectation.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
        In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1214251]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found