The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Lvalue - add lvalue getters and setters to existing objects


version 0.21


Lvalue takes an object produced by some other package and wraps it with lvalue functionality implemented with the object's original getter and setter routines. Lvalue assumes its object uses the relatively standard getter / setter idiom where any arguments is a setter, and no arguments is a getter.

By wrapping an existing object's getters and setters, Lvalue gives you the syntactic niceties of lvalues, without the inherent encapsulation violations of the :lvalue subroutine attribute.

    my $obj = NormalObject->new();


    print $obj->value(); # prints 5

    use Lvalue;

    Lvalue->wrap( $obj );

    $obj->value = 10;

    print $obj->value;   # prints 10

    $_ += 2 for $obj->value;

    print $obj->value;   # prints 12


this module does not export anything by default but can export the functions below (which can all also be called as methods of Lvalue)

    use Lvalue qw/lvalue/; # or 'wrap', also 'unwrap'/'rvalue'

    lvalue my $obj = SomePackage->new;

    $obj->value = 5;

    Lvalue->unwrap( $obj );

    $obj->value = 6; # dies


lvalue OBJECT

wrap an object with lvalue getters / setters

    my $obj = Lvalue->wrap( SomePackage->new );

or in a constructor:

    sub new {
        my $class = shift;
        my $self  = {@_};
        Lvalue->wrap( bless $self => $class );

in void context, an in-place modification is done:

    my $obj = SomePackage->new;

    Lvalue->wrap( $obj );

    $obj->value = 5;

the alias lvalue is provided for wrap which when you export it as a function, can lead to some nice code:

    use NormalObject;
    use Lvalue 'lvalue';

    lvalue my $obj = NormalObject->new;

    $obj->value = 5;

returns the original object


Eric Strom, <asg at>


special care is taken to ensure that overloaded objects still work properly. if you encounter an error please let me know.

Please report any bugs or feature requests to bug-lvalue at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


Copyright 2010 Eric Strom.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See for more information.