The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Focus::Lens::Accessor - lens for "typical" accessor methods

SYNOPSIS

    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");

DESCRIPTION

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.

CLASS METHOD

$lens = Data::Focus::Lens::Accessor->new(%args)

The constructor.

Fields in %args are:

method => STR (mandatory)

Accessor method name.

AUTHOR

Toshio Ito <debug.ito at gmail.com>