| 1 |
NAME |
| 2 |
LV - LV ♥ lvalue |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use LV qw( lvalue get set ); |
| 6 |
|
| 7 |
my $xxx; |
| 8 |
sub xxx :lvalue { |
| 9 |
lvalue |
| 10 |
get { $xxx } |
| 11 |
set { $xxx = $_[0] } |
| 12 |
} |
| 13 |
|
| 14 |
xxx() = 42; |
| 15 |
say xxx(); # says 42 |
| 16 |
|
| 17 |
DESCRIPTION |
| 18 |
This module makes lvalue subroutines easy and practical to use. It's |
| 19 |
inspired by the lvalue module which is sadly problematic because of the |
| 20 |
existence of another module on CPAN called Lvalue. (They can get confused |
| 21 |
on file-systems that have case-insensitive file names.) |
| 22 |
|
| 23 |
LV comes with three different implementations, based on Variable::Magic, |
| 24 |
Sentinel and `tie`; it will choose and use the best available one. You can |
| 25 |
force LV to pick a particular implementation using: |
| 26 |
|
| 27 |
$ENV{PERL_LV_IMPLEMENTATION} = 'Magic'; # or 'Sentinel' or 'Tie' |
| 28 |
|
| 29 |
The tie implementation is the slowest, but will work on Perl 5.6 with only |
| 30 |
core modules. |
| 31 |
|
| 32 |
Functions |
| 33 |
`lvalue(%args)` |
| 34 |
Creates the magic lvalue. This must be the last expression evaluated |
| 35 |
by the lvalue sub (and thus will be returned by the sub) but also must |
| 36 |
not be returned using an explicit `return` keyword (which would break |
| 37 |
its lvaluedness). |
| 38 |
|
| 39 |
As a matter of style, you may like to omit the optional semicolon |
| 40 |
after calling this function, which will act as a reminder that no |
| 41 |
statement should follow this one. |
| 42 |
|
| 43 |
The arguments are `get` and `set`, which each take a coderef: |
| 44 |
|
| 45 |
sub xxx :lvalue { |
| 46 |
lvalue( |
| 47 |
get => sub { $xxx }, |
| 48 |
set => sub { $xxx = $_[0] }, |
| 49 |
); # semicolon |
| 50 |
} |
| 51 |
|
| 52 |
Note that the `set` coderef gets passed the rvalue part as $_[0]. |
| 53 |
|
| 54 |
`get { BLOCK }`, `set { BLOCK }` |
| 55 |
Convenience functions for defining `get` and `set` arguments for |
| 56 |
`lvalue`: |
| 57 |
|
| 58 |
sub xxx :lvalue { |
| 59 |
lvalue |
| 60 |
get { $xxx } |
| 61 |
set { $xxx = $_[0] } |
| 62 |
} |
| 63 |
|
| 64 |
As well as populating %args for `lvalue`, these functions also use |
| 65 |
Sub::Name (if it's installed) to ensure that the anonymous coderefs |
| 66 |
have sensible names for the purposes of stack traces, etc. |
| 67 |
|
| 68 |
These functions are not exported by default. |
| 69 |
|
| 70 |
`implementation()` |
| 71 |
Can be used to determine the current backend. |
| 72 |
|
| 73 |
Cannot be exported. |
| 74 |
|
| 75 |
BUGS |
| 76 |
Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=LV>. |
| 77 |
|
| 78 |
SEE ALSO |
| 79 |
lvalue, Sentinel. |
| 80 |
|
| 81 |
AUTHOR |
| 82 |
Toby Inkster <[email protected]>. |
| 83 |
|
| 84 |
COPYRIGHT AND LICENCE |
| 85 |
This software is copyright (c) 2013 by Toby Inkster. |
| 86 |
|
| 87 |
This is free software; you can redistribute it and/or modify it under the |
| 88 |
same terms as the Perl 5 programming language system itself. |
| 89 |
|
| 90 |
DISCLAIMER OF WARRANTIES |
| 91 |
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 92 |
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 93 |
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 94 |
|