NAME

Data::Focus::LensMaker - utility to make your own lens

SYNOPSIS

package Person;

sub new {
    my ($class, $first_name, $last_name) = @_;
    return bless {
        first_name => $first_name,
        last_name => $last_name,
    }, $class;
}

sub first_name {
    my $self = shift;
    $self->{first_name} = $_[0] if @_;
    return $self->{first_name};
}

package Person::Lens::FirstName;
use parent qw(Data::Focus::Lens);
use Data::Focus::LensMaker qw(make_lens_from_accessors);

sub new {
    my ($class) = @_;
    my $self;
    return bless \$self, $class;
}

sub _getter {
    my ($self, $target) = @_;
    return $target->first_name;
}

sub _setter {
    my ($self, $target, $set) = @_;
    $target->first_name($set);
    return $target;
}

make_lens_from_accessors(\&_getter, \&_setter);

DESCRIPTION

Data::Focus::LensMaker is a helper module to create lens classes.

EXPORTABLE FUNCTIONS

The following functions are exported only by request.

make_lens_from_accessors($getter, $setter)

Generate necessary methods for a Data::Focus::Lens class from $getter and $setter, and install them in the calling package. See "SYNOPSIS" for usage.

$getter and $setter are code-refs. They are supposed to be getter and setter methods defined in your lens class, respectively.

$getter is supposed to extract the focused parts from the target data.

@parts = $lens_self->$getter($target)

$getter is called in the list context. The number of @parts determines the number of focal points the lens creates on the $target.

$setter is supposed to set parts into the $target, and return the result.

$modified_target = $lens_self->$setter($target, @parts)

where @parts are the data parts to be set to the $target. @parts has the same length and order as the one retuned by $getter.

It's up to the $setter whether the operation is destructive or not. If you modify the $target itself in the $setter, the lens is destructive.

Note that $setter is called with empty @parts if $getter returns an empty list. In that case, $setter should return $target unmodified or its clone.

AUTHOR

Toshio Ito, <toshioito at cpan.org>