Data::Focus::Lens::Accessor - lens for "typical" accessor methods
package Person; sub new { my ($class, $name) = @_; return bless { name => $name }, $class; } sub name { my ($self, $v) = @_; $self->{name} = $v if @_ > 1; return $self->{name}; } package main; use Data::Focus qw(focus); use Data::Focus::Lens::Accessor; my $target = Person->new("john"); my $name_lens = Data::Focus::Lens::Accessor->new(method => "name"); focus($target)->get($name_lens); ## => "john" focus($target)->set($name_lens, "JOHN");
This is an implementation of Data::Focus::Lens, which focuses on an accessor method of the target object. It assumes the typical accessor method signature widely used in Perl:
my $got_value = $target->accessor_method; ## getter $target->accessor_method($set_value); ## setter
It creates no focal points if the $target is non-blessed or it doesn't have the specified accessor method.
$target
The constructor.
Fields in %args are:
%args
method
Accessor method name.
Toshio Ito <debug.ito at gmail.com>
<debug.ito at gmail.com>
To install Data::Focus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Focus
CPAN shell
perl -MCPAN -e shell install Data::Focus
For more information on module installation, please visit the detailed CPAN module installation guide.