LV(3pm) | User Contributed Perl Documentation | LV(3pm) |
LV - LV ♥ lvalue
use LV qw( lvalue get set ); my $xxx; sub xxx :lvalue { lvalue get { $xxx } set { $xxx = $_[0] } } xxx() = 42; say xxx(); # says 42
This module makes lvalue subroutines easy and practical to use. It's inspired by the lvalue module which is sadly problematic because of the existence of another module on CPAN called Lvalue. (They can get confused on file-systems that have case-insensitive file names.)
LV comes with three different implementations, based on Variable::Magic, Sentinel and "tie"; it will choose and use the best available one. You can force LV to pick a particular implementation using:
$ENV{PERL_LV_IMPLEMENTATION} = 'Magic'; # or 'Sentinel' or 'Tie'
The tie implementation is the slowest, but will work on Perl 5.6 with only core modules.
As a matter of style, you may like to omit the optional semicolon after calling this function, which will act as a reminder that no statement should follow this one.
The arguments are "get" and "set", which each take a coderef:
sub xxx :lvalue { lvalue( get => sub { $xxx }, set => sub { $xxx = $_[0] }, ); # semicolon }
Note that the "set" coderef gets passed the rvalue part as $_[0].
sub xxx :lvalue { lvalue get { $xxx } set { $xxx = $_[0] } }
As well as populating %args for "lvalue", these functions also use Sub::Name (if it's installed) to ensure that the anonymous coderefs have sensible names for the purposes of stack traces, etc.
These functions are not exported by default.
Cannot be exported.
Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=LV>.
lvalue, Sentinel.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2022-08-28 | perl v5.34.0 |