Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by BrowserUk (Patriarch)
on May 09, 2018 at 11:23 UTC ( [id://1214271]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: can sub check context for lvalue vs rvalue context?
by perl-diddler (Chaplain) on May 10, 2018 at 07:00 UTC
    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
        I have a feeling we are not using the same terms as having the same meaning.

        When I return an lvalue, in my mind, I'm returning the address of an object so that the compiler/interpreter can put something in it (has happens to the left side of an assignment).

        Vs. if I return an rvalue, In my terminology, I'm only returning the value that was in that address -- so the compiler/interpreter can't assign a new value into the variable location.

        If I return an lvalue, the compiler can store a new value. But it can also propagate that value to another lvalue. No problem: I don't care. What I care about is detecting that my lvalue sub has been assigned to (whether or not the value changes is irrelevant).

        The rvalue context I would detect in a statement like:

        if ($p->url) {...}
        would allow me to return an actual value and not a tied var.

        It's the 'lvalue' case where I need to presume that the value in 'url might change, and thus return a more expensive (ex. tied) version so I can update the url's component values.

        I don't see how that is problematic.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-20 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found