Data::Focus::Lens::HashArray::Index - a lens to focus on element(s) of hash/array
use Data::Focus qw(focus); use Data::Focus::Lens::HashArray::Index; sub lens { Data::Focus::Lens::HashArray::Index->new(index => $_[0]) } my $target = { foo => "bar", hoge => [ "a", "b", "c" ] }; focus($target)->get(lens("foo")); ## => "bar" focus($target)->get(lens("hoge")); ## => ["a", "b", "c"] focus($target)->get(lens("hoge"), lens(1)); ## => "b" focus($target)->list(lens("hoge"), lens([0, 2])) ## => ("a", "c")
This is an implementation of Data::Focus::Lens, which focuses on one or more elements in a hash or array.
Conceptually, this lens does the same as hash/array subscripts and slices.
$hash->{index} @{$hash}{"index1", "index2", "index3"} $array->[4] @{$array}[3,4,5]
This lens never autovivifies when reading, while it DOES autovivify when writing.
Detailed behaviors of this lens are described below for each target type.
If the target is a hash-ref, this lens behaves as its subscript or slice.
Duplicate keys in a slice are allowed. If different values are set to those keys, only the last one takes effect.
It returns undef for non-existent keys. You can set values to them.
undef
If the target is an array-ref, this lens behaves as its subscript and slice. The indices are cast to integers.
Positive out-of-range indices are allowed. get() and list() return undef for those indices. When set, it extends the array.
get()
list()
Negative indices are allowed. They create focal points from the end of the array, e.g., index of -1 means the last element in the array.
-1
Negative out-of-range indices are read-only. They always return undef. If you try to set values, it croaks.
Duplicate indices in a slice are allowed. If different values are set to those indices, only the last one takes effect.
When reading, it always returns undef.
When writing, it autovivifies an array-ref if and only if the indices are all non-negative integers. Otherwise, it autovivifies a hash-ref.
By default, the lens creates no focal point for a blessed target. This means get() returns undef and set() does nothing.
set()
If allow_blessed option is set to true and the target is made of a hash-ref or array-ref, the lens creates focal points as if the target were a regular hash-ref or array-ref.
allow_blessed
For other types of targets such as scalar-refs, the lens creates no focal point. This means get() returns undef and set() does nothing.
The constructor. Fields in %args are:
%args
index
Index to focus. When you specify an array-ref, the $lens behaves like slice.
$lens
immutable
If set to true, the target hash/array is treated as immutable. This means every updating operation using the $lens creates a new hash/array in a copy-on-write fashion.
If set to true, the lens makes focal points for blessed targets if they are made of hash-refs or array-refs.
You should not set this option together with immutable option, bacause in this case you get a plain (unblessed) hash-ref/array-ref from set() method. This is confusing.
See Data::Focus::Lens.
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.