Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Is ": lvalue" attribute usefull or something to avoid?

by BrowserUk (Patriarch)
on Feb 12, 2014 at 04:58 UTC ( [id://1074584]=note: print w/replies, xml ) Need Help??


in reply to Is ": lvalue" attribute usefull or something to avoid?

There are two main problems with lvalue subroutines:

  1. It is impossible to validate the assigned value.

    You cannot inspect the value before the assignment as it isn't accessible.

    You cannot check the value after the assignment but before returning, because the lvalue that receives the assignment must be the last thing in the subroutine before it returns.

  2. The lvalue that receives the assignment must be the last thing in the subroutine.

    Even without considering the first problem above, you often would like to be able to do other things before returning.


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".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: Is ": lvalue" attribute usefull or something to avoid?

Replies are listed 'Best First'.
Re^2: Is ": lvalue" attribute usefull or something to avoid?
by tobyink (Canon) on Feb 12, 2014 at 12:01 UTC

    It is possible to validate the values, it's just traditionally a pain in the arse, because it involves using something like Variable::Magic or returning a tied scalar.

    I wrote LV to try to get around these issues, and it does indeed make writing lvalue subs pretty friendly. The primary drawback becomes speed - none of the implementations are as fast as a more traditionally written accessor.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      It is possible to validate the values, it's just traditionally a pain in the arse, because it involves using something like Variable::Magic or returning a tied scalar.

      Yes. I've done it using a tied scalars, but the penalties -- not just performance, but the obfuscation factor -- makes it a complete non-starter IMO.

      none of the implementations are as fast as a more traditionally written accessor.

      If the performance penalty was a few percent, the syntactic sugar might be viable; but when it is closer 200% or 300% for subroutines/methods that already have a high overhead to work-done ratio, it simply isn't worth it.


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        300%? In my experience it's often a lot more than that!

        That said, our old friend Class::XSAccessor to the rescue! If you're writing OO code, want lvalue accessors, and don't give a crap about validating incoming data, then its lvalue accessors are likely to be much faster than any pure Perl accessor you could write. (Not quite as fast as Class::XSAccessors non-lvalue accessors though.)

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-24 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found