Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
All,

Lvalue subs are experimental. It says so right in perldoc perlsub. They have been experimental since their introduction in 1999, nearly 6 years ago. Everytime someone asks how to do argument validation with lvaluable subs, the mantra is "lvaluable subs are experimental and shouldn't be used, there are modules that likely do what you want already, but if you must do that then you will need to tie the return variable which is going to make it slow".

While not exactly the same, this is similar to the response about using map in a void context. Readability issues aside, the efficiency problem turned out to be a 3 line patch. I feel like the same thing is happening with Lvalue subs and am interested in what you think. Consider the following snippet:

# Somewhere inside the object's package sub get_set :lvalue { my ($self, $key) = @_; $self->{ $key }; } # Your code that uses it my $obj = Some::Object->new(); $obj->get_set( 'name' ) = 'Limbic'; print "Hello, ", $obj->get_set( 'name' ), "\n";
It allows me to use a single accessor/mutator method. Of course, the same thing could be done as
$obj->get_set('name' => 'Limbic')
but some people really prefer the assignment notation. So what's the problem? Well, first I might want to restrict what keys can actually be set. Borrowing a trick from Zaxo, you can do this with the current Lvalue implementation:
sub get_set :lvalue { my ($self, $key) = @_; my $junk; key_ok( $key ) ? $self->{ $key } : $junk; }

While this will silently doesn't do what you asked, it could be made to be verbose but it still doesn't allow you to validate the rvalue. It isn't on the @_ stack. In fact, as far as I know, there is no way to get to the rvalue inside an Lvalue sub. Have given this some thought, discussing it in the CB, thrown out some possible implementations, I think there are only a couple of things that are necessary to make Lvalue subs more useable.

  • A way to tell if it is being used in assignment or not
  • When in assignment, a way to get at the rvalue(s)

Of course there are a few other things that might make them nicer. A straight forward alternative to using a $junk variable and ternary to avoid assignment if validation fails would be nice. I would imagine that you could manipulate the rvalue(s) prior to the assignment, but it might also be nice to have pre and post hooks for syntatic sugar purposes.

I am not saying that these changes would only be 3 lines but is that any reason not to make them more useable? When I mentioned in the CB that the problem was having a competent C programmer write the patch and then having the fortitude to fight for it on p5p. The response from a monk who has been around the Perl community for quite some time and has been involved with p5p indicated the real problem was the latter not the former. I find this hard to understand. If there is no currently promised behavior and someone provides a working patch to make it useable, why not apply it?

So what do you think - about extending Lvalue subs at all, about the difficulty in doing so both from a C perspective as well as getting the patch applied, about what features and implementation would really make it worth the effort?

Cheers - L~R


In reply to Experimenting with Lvalue Subs by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (7)
    As of 2014-07-31 22:37 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (254 votes), past polls