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(); $obj->value(5); 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
wrap OBJECT
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:
lvalue
wrap
use NormalObject; use Lvalue 'lvalue'; lvalue my $obj = NormalObject->new; $obj->value = 5;
unwrap LVALUE_OBJECT
rvalue LVALUE_OBJECT
returns the original object
Eric Strom, <asg at cpan.org>
<asg at cpan.org>
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 rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Lvalue. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-lvalue at rt.cpan.org
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 http://dev.perl.org/licenses/ for more information.
To install Lvalue, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Lvalue
CPAN shell
perl -MCPAN -e shell install Lvalue
For more information on module installation, please visit the detailed CPAN module installation guide.