Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by TomDLux (Vicar)
on Feb 12, 2014 at 04:25 UTC ( #1074581=perlquestion: print w/replies, xml ) Need Help??

TomDLux has asked for the wisdom of the Perl Monks concerning the following question:

I'm watching Steve Baker's Perl Attributes presentation in Phoenix. He mentions the predefined attribute 'lvalue', which is an alternative to passing an assignment value as a function argument. In a package definition ....

sub value : lvalue { my $self = shift; return $self->{value} } .... $obj->value() = 42; # compare with boring assignment ... $obj->value(42)

Is lvalue useful? are people using it? or is it just a dud?

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Replies are listed 'Best First'.
Re: Is ": lvalue" attribute usefull or something to avoid?
by BrowserUk (Pope) on Feb 12, 2014 at 04:58 UTC

    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.

      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.
Re: Is ": lvalue" attribute usefull or something to avoid?
by tobyink (Canon) on Feb 12, 2014 at 12:03 UTC

    Actually that sub won't work. For lvalue subs you should not use a return statement. You need something like:

    sub value : lvalue { my $self = shift; $self->{value}; }
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: Is ": lvalue" attribute usefull or something to avoid?
by Anonymous Monk on Feb 12, 2014 at 04:55 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2020-09-27 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (142 votes). Check out past polls.

    Notices?