NAME

LV - LV ♥ lvalue

SYNOPSIS

use LV qw( lvalue get set );

my $xxx;
sub xxx :lvalue {
   lvalue
      get { $xxx }
      set { $xxx = $_[0] }
}

xxx() = 42;
say xxx();    # says 42

DESCRIPTION

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.

Functions

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).

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].

get { BLOCK }, set { BLOCK }

Convenience functions for defining get and set arguments for 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.

These functions are not exported by default.

implementation()

Can be used to determine the current backend.

Cannot be exported.

BUGS

Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=LV.

SEE ALSO

lvalue, Sentinel.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

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.

DISCLAIMER OF WARRANTIES

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.