NAME
Data::CSel::WrapStruct - Wrap data structure into a tree of objects suitable for use with Data::CSel
VERSION
This document describes version 0.004 of Data::CSel::WrapStruct (from Perl distribution Data-CSel-WrapStruct), released on 2019-07-29.
SYNOPSIS
use Data::CSel qw(csel);
use Data::CSel::WrapStruct qw(wrap_struct);
my $data = [
0,
1,
[2, ["two","dua"], {url=>"http://example.com/two.jpg"}, ["even","prime"]],
3,
[4, ["four","empat"], {}, ["even"]],
];
my $tree = wrap_struct($data);
my @nodes = csel(":root > * > *:nth-child(4) > *", $tree);
my @tags = map { $_->value } @nodes; # -> ("even", "prime", "even")
Arrays are wrapped in a Data::CSel::WrapStruct::Array, hashes in a Data::CSel::WrapStruct::Hash, and so on. So if you are using type selectors, you might want to add Data::CSel::WrapStruct
into class_prefixes
for convenience:
my @hashes = map {$_->value} csel({class_prefixes=>["Data::CSel::WrapStruct"]}, "Hash", $tree);
# -> ({url=>"http://example.com/two.jpg"}, {})
The wrapper objects provide some methods, e.g.:
my @empty_hashes = map {$_->value} csel({class_prefixes=>["Data::CSel::WrapStruct"]}, "Hash[length=0]", $tree);
# -> ({})
Refer to their respective documentation for the list of methods available.
Some more examples:
[map {$_->value} csel({class_prefixes=>["Data::CSel::WrapStruct"]}, "Scalar[value >= 3]")]
# -> (3, 4)
DESCRIPTION
This module provides wrap_struct()
which creates a tree of objects from a generic data structure. You can then perform node selection using Data::CSel's csel()
.
You can retrieve the original value of data items by calling value()
method on the tree nodes.
FUNCTIONS
None exported by default, but exportable.
wrap_struct($data) => tree
Wrap a data structure using a tree of objects.
Currently cannot handle recursive structure.
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Data-CSel-WrapStruct.
SOURCE
Source repository is at https://github.com/perlancar/perl-Data-CSel-WrapStruct.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Data-CSel-WrapStruct
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
SEE ALSO
AUTHOR
perlancar <perlancar@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2019, 2016 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.