Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by BrowserUk (Patriarch)
on May 12, 2018 at 07:44 UTC ( [id://1214414]=note: print w/replies, xml ) Need Help??


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

but in non-lvalue context, I can return the variable directly in the belief that it is not being modified or assigned to.

Your belief is wrong. In order for Perl to determine if the lvalue you return is ever modified, it would have to trace every usage or reference or alias of that variable right through to the end of the program.

Eg.

sub x :lvalue { ...; $lvalue } ... $y = this( $_ ) ? that( $_ ) : tother( $_ ) for x();

You may think that is too elaborate an example that will never come up; but to be useful, your wantlvalue() would need to handle that and every other possibility.

Even the 'simple case', of somefunc( x() ); conceals the fact that the lvalue returned by x() is aliased, and within somefunc() could be passed on to still more code as an alias or reference.

For your "belief that it is not being modified or assigned to" to have any validity, Perl would need to follow every possible path to the end of the program before it decide whether to return true or false from wantlvalue().

Imagine the lvalue (or an alias or reference to it) is used in a conditional statement inside a loop; that could be dozens (hundreds/millions) of different possible paths, any of which might modify it.

It would be impossible for a dynamic, interpreted language to provide an "is never modified" guarantee.

And that's before you consider the possibility that an alias or reference to your lvalue might be used as part of a piece of run-time constructed code that gets eval'd.


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^12: can sub check context for lvalue vs rvalue context?
by perl-diddler (Chaplain) on May 14, 2018 at 03:59 UTC
    In non-lvalue context, you can return the direct variable. It doesn't matter if the lvalue is "ever" modified -- only at that point when it is dereferenced. It isn't returned if you are taking a reference to it.

    Only in every instance where you dereference it, does it THEN determine context. But passing around a reference is a null-op as far as the object goes. The code in it won't get called as long as one is only taking references to it and passing them around as references or aliases.

      In non-lvalue context, you can return the direct variable. It doesn't matter if the lvalue is "ever" modified -- only at that point when it is dereferenced. It isn't returned if you are taking a reference to it.

      Sorry, but I think I've been more than patient. That's BS!

      Its BS, because you cannot ever know if it is called "In non-lvalue context". Perl provides no mechanism to give you that information.

      Wishing it were so does not make it so. Stridently concluded that "you can"; on the basis of your fantasy that you could if perl gave you the information; is stupid.

      Stupid, because Perl cannot give you that information!.

      Another attempt. This time, try reading the code and understanding it -- not your fantasy vision of it:

      sub x :lvalue{ ...; $something } ... someFunc( x() );

      The value returned from x() is passed to somefunc() as $_[0]; if somefunc() modifies $_[0], it also modifies $something within x(). Perl cannot know if it does!

      And someFunc() might do someOtherFunc( $_[0] ); And if someOtherFunc() modifies it's version of $_[0], it will also modify $something in x().

      And if someOtherFunc() calls someOtherOtherFunc( $_[0] ); And if someOtherOtherFunc() modifies it's version of $_[0], then it will also modify $something.

      And if someOtherOtherFunc()....

      It is not possible for Perl to delve into all the calling code; and all the code it calls, and all the code that code calls through to the end of the program run to determine if anything will/could/might modify the lvalue x() returns.

      And wish and prevaricate as much as you like; it ain't never gonna happen.


      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://1214414]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-24 14:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found