I don't understand what could be the uses of lvalue subroutines? What is it that I can't accomplish with normal subroutines? Could you please post some examples?
Thanks
Perl, C) An lvalue is an expression that you can write on the left hand side of an assignment statement - in other words an expression that defines a specific memory address of a variable. The most common lvalues are simple variables or array / list / hash / dictionary members ...
A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again.
The first thing you need to do is create a subroutine. sub keyword is used to define a subroutine in Perl program.
Subroutines are blocks of code that can be reused across programs. They are the same as functions or user-defined functions in Perl. We can either define a subroutine in the same program or import it from another file using the use , do , or require statements.
You can use lvalue subs as setters for object members:
sub x : lvalue {
my $self = shift;
$self->{x};
}
Then later:
$foo->x = 5;
This combines the intuitiveness of setting members directly with the flexibility of encapsulating the setter, in case you later want to change how the object is implemented. Compare to the following more traditional ways to set members in Perl:
Intuitive but fragile; what if you change how $foo is implemented?
$foo->{x} = 5;
Traditional setter method; doesn't use assignment syntax for what amounts to an assignment:
$foo->set_x(5);
Of course, lvalue methods have never really been embraced by the Perl community, and use of lvalue subs is "weird" and whatever would be gained for intuitiveness would be lost by the strangeness of it all.
LValues are recommended to be avoided. They're fun and all, but they confuse new users who make assumptions about how all subs work, and thus it should be avoided in code when a better way is available.
Sometimes it can be extremely practical to use an LValue result to do dirty work.
substr( $string, $start, $stop ) = 'somevalue' # replaces the specified part of the substring.
However, this also does the same thing:
substr( $string, $start, $stop , 'somevalue' );
The difference here being mostly the former has significantly more expressive power, as it delegates the behaviour to the code that uses it instead of the function itself.
For instance:
substr( $string, $start, $stop ) =~ s/a/b/g
Would require you to do
my $x = substr( $string, $start, $stop );
$x =~ s/a/b/g/;
substr( $string, $start, $stop, $x );
Which is a bit nasty.
Although, there's an additional nasty here, because you have to think about whether doing that substitution or not with $x works retroactively to modify the string or not ( due to substr being an LValue sub). I don't think it does, but Its ambiguous enough I have to check the manual to remember whether it does or not, and that's bad.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With