Data::Focus::LensMaker - utility to make your own lens
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);
Data::Focus::LensMaker is a helper module to create lens classes.
The following functions are exported only by request.
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
$setter
$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.
@parts
$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.
Toshio Ito, <toshioito at cpan.org>
<toshioito at cpan.org>
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.