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:
tie
$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.
lvalue(%args)
Creates the magic lvalue. This must be the last expression evaluated by the lvalue sub (and thus will be returned by the sub) but also must not be returned using an explicit return keyword (which would break its lvaluedness).
return
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:
get
set
sub xxx :lvalue { lvalue( get => sub { $xxx }, set => sub { $xxx = $_[0] }, ); # semicolon }
Note that the set coderef gets passed the rvalue part as $_[0].
$_[0]
get { BLOCK }
set { BLOCK }
Convenience functions for defining get and set arguments for lvalue:
lvalue
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.
%args
These functions are not exported by default.
implementation()
Can be used to determine the current backend.
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.
To install LV, copy and paste the appropriate command in to your terminal.
cpanm
cpanm LV
CPAN shell
perl -MCPAN -e shell install LV
For more information on module installation, please visit the detailed CPAN module installation guide.