LV(3pm)

LV3pm

Lv ♥ lvalue

Usage

   use LV qw( lvalue get set );
   
   my $xxx;
   sub xxx :lvalue {
      lvalue
         get { $xxx }
         set { $xxx = $_[0] }
   }
   
   xxx() = 42;
   say xxx();    # says 42

Overview

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.

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.

Releases

3 releases since Debian 11 (Bullseye); the text changed 1 time.

  1. 11
  2. 12–13

Bugs

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

Author

Toby Inkster <[email protected]>.

From liblv-perl 0.006-4 in Debian 13 (Trixie). Parsed by carta from the page's man source.