Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Experimenting with Lvalue Subs

by BrowserUk (Pope)
on Jan 24, 2005 at 18:18 UTC ( #424648=note: print w/ replies, xml ) Need Help??


in reply to Experimenting with Lvalue Subs

I totally agree, and said as much in What the [sub]in 'L value do they have?. I even suggested a few ways that the value could be made available to the sub without breaking existing code:

  1. By placing it into the scalar slot associated of the typeglob of the functions name.
  2. By placing it into $^_.

    This is analogous to $^N as used in the (??{ code }) regex construct and is mnemonic* with $_.

    Corrected per dragonchild's post below.

    It could also be extended to @^_ for lvalue list assignment--which has interesting possibilities.

I think it probably would just require someone with the tuits to make such modifications to the sourcecode to produce the patch.


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.


Comment on Re: Experimenting with Lvalue Subs
Download Code
Re^2: Experimenting with Lvalue Subs
by dragonchild (Archbishop) on Jan 24, 2005 at 18:23 UTC
    By placing it into the scalar slot associated of the typeglob of the functions name.

    That's not so good - you run the risk of clobbering something. Doing an implicit 'local' might help, but what do you do with lvalue-able anonymous subs?

    *^_ is a much better idea, imho. And, I'd just have it go to @^_ straight off. That way, it would act as a standard assignment does. $foo = 1..3; anyone?

    (FYI - it's mnemonic, not pnemonic ... you want memory, not the plague.)

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      ...but what do you do with lvalue-able anonymous subs?

      Good point. Then I agree, @^_ is best.


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.
Re^2: Experimenting with Lvalue Subs
by fergal (Chaplain) on Jan 24, 2005 at 20:35 UTC
    I thought about that too but one problem is order of evaluation. In order to provide the rvalue to the sub, you would have to evaluate the right hand side before the left hand side and that's odd or possibly even bad (I'm not exactly sure yet).

    I've also seen Larry Wall say somewhere that tieing is the way you will have to do this in Perl 6 (although it will be considerably more straight forward that in Perl 5)

      ...you would have to evaluate the right hand side before the left hand side...

      I'm not sure that I understand you. The right-hand side of an assignment is always evaluated before the assignment can occur? Except maybe in the case of assigning a constant, but even then the value of that constant is fetched from wherever before it can be assigned to the lvalue.

      It's quite possible that I do not fully grasp the purpose or magic that lies behind the implementation of lvalue subs, but any code in an lvalue sub is invoked as a sideeffect of the assignment to an lvalue sub:

      { my $x; sub x: lvalue { print 'fred'; $x } }; x = 123; fred

      The problem is that you can't do anything useful with that code as you cannot affect the outcome of the assignment relative to the value being assigned.


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.
        Yes, that part was nonsense, sorry about that. However Larry has officially declared tie as the way to do it for other reasons (see my reply to LR above).
Re^2: Experimenting with Lvalue Subs
by Anonymous Monk on Jan 25, 2005 at 12:16 UTC
    By placing it into the scalar slot associated of the typeglob of the functions name.
    You can't. It has already been taken by AUTOLOAD.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2014-10-23 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls