NAME
Data::Walk::Prune - A way to say what should be removed
SYNOPSIS
#!perl
use Modern::Perl;
use Moose::Util qw( with_traits );
use lib '../lib';
use Data::Walk::Extracted v0.015;
use Data::Walk::Prune v0.007;
use Data::Walk::Print v0.009;
my $edward_scissorhands = with_traits(
'Data::Walk::Extracted',
(
'Data::Walk::Prune',
'Data::Walk::Print',
),
)->new( change_array_size => 1, );#Default
my $firstref = {
Helping => [
'Somelevel',
{
MyKey => {
MiddleKey => {
LowerKey1 => 'low_value1',
LowerKey2 => {
BottomKey1 => 'bvalue1',
BottomKey2 => 'bvalue2',
},
},
},
},
],
};
my $result = $edward_scissorhands->prune_data(
tree_ref => $firstref,
slice_ref => {
Helping => [
undef,
{
MyKey => {
MiddleKey => {
LowerKey1 => {},
},
},
},
],
},
);
$edward_scissorhands->print_data( $result );
######################################################################################
# Output of SYNOPSIS
# 01 {
# 02 Helping => [
# 03 'Somelevel',
# 04 {
# 05 MyKey => {
# 06 MiddleKey => {
# 07 LowerKey2 => {
# 08 BottomKey1 => 'bvalue1',
# 09 BottomKey2 => 'bvalue2',
# 10 },
# 12 },
# 13 },
# 14 },
# 15 ],
# 16 },
######################################################################################
DESCRIPTION
This Moose::Role contains methods for implementing the method "prune_data" using Data::Walk::Extracted. By sending a slice_ref that terminates in an empty hash_ref (no keys) or an empty array_ref (no positions) then the tree_ref will be pruned at that spot. 'prune_data' returns a deep cloned data ref that matches the 'tree_ref' with the 'slice_ref' bits taken off.
Caveat utilitor
Because this uses Data::Walk::Extracted the final $tree_ref is a deep cloned ref with no data pointers matching the original $tree_ref.
Supported Node types
- ARRAY
- HASH
- SCALAR
Supported one shot "Attributes"
- prune_memory
USE
This is a Moose::Role and can be used as such. One way to use this role with Data::Walk::Extracted, is the method 'with_traits' from Moose::Util. Otherwise see Moose::Manual::Roles.
Methods
prune_data( %args )
- Definition: This is a method used to remove targeted parts of a data reference.
- Accepts: a hash ref with the keys 'slice_ref' and 'tree_ref' (both required). The slice ref can contain more than one 'slice' location in the data reference.
-
- tree_ref This is the primary data ref that will be manipulated and returned changed.
- slice_ref This is a data ref that will be used to prune the 'tree_ref'. In general the slice_ref should match the tree_ref for positions that should remain unchanged. Where the tree_ref should be trimmed insert either an empty array ref or an empty hash ref. If this position represents a value in a hash key => value pair then the hash key is deleted. If this position represents a value in an array then the position is deleted/cleared depending on the attribute 'change_array_size' in Data::Walk::Extracted. If the slice ref diverges from the tree ref then no action is taken past the divergence, even if there is a mandated slice. (no defacto grafting occurs!)
- [attribute name] - attribute names are accepted with temporary attribute settings. These settings are temporarily set for a single "prune_data" call and then the original attribute values are restored. For this to work the the attribute must meet the necessary criteria.
- Example
-
$pruned_tree_ref = $self->prune_data( tree_ref => $tree_data, slice_ref => $slice_data, prune_memory => 0, );
- Returns: The $tree_ref with any changes (Deep cloned)
set_prune_memory( $Bool )
- Definition: This will change the setting of the "prune_memory" attribute.
- Accepts: 1 = remember | 0 = no memory
- Returns: nothing
get_prune_memory
- Definition: This will return the current setting of the "prune_memory" attribute.
- Accepts: nothing
- Returns: A $Bool value for the current state
has_prune_memory
- Definition: This will indicate if the "prune_memory" attribute is set
- Accepts: nothing
- Returns: A $Bool value 1 = defined, 0 = not defined
clear_prune_memory
- Definition: This will clear the "prune_memory" attribute value (Not the actual prune memory)
- Accepts: nothing
- Returns: A $Bool value 1 = defined, 0 = not defined
has_pruned_positions
- Definition: This answers if any pruned positions were stored
- Accepts: nothing
- Returns: A $Bool value 1 = pruned cuts are stored, 0 = no stored cuts
get_pruned_positions
- Definition: This returns an array ref of stored pruning cuts
- Accepts: nothing
- Returns: an ArrayRef - even if the cuts were defined in one data ref this will return one data ref per cut. Each ref will go to the root of the original data ref.
number_of_cuts
Attributes
Data passed to ->new when creating an instance using a class. For modification of these attributes see "Methods". The ->new function will either accept fat comma lists or a complete hash ref that has the possible appenders as the top keys. Additionally some attributes that have all the following methods; get_$attribute, set_$attribute, has_$attribute, and clear_$attribute, can be passed to prune_data and will be adjusted for just the run of that method call. These are called 'one shot' attributes.
prune_memory
- Definition: When running a prune operation any branch called on the pruner that does not exist in the tree will not be used. This attribute turns on tracking of the actual cuts made and stores them for review after the method is complete. This is a way to know if the cut was actually implemented.
- Default undefined
- Range 1 = remember the cuts | 0 = don't remember
Attributes in Data::Walk::Extracted - also affect the output.
GLOBAL VARIABLES
- $ENV{Smart_Comments}
-
The module uses Smart::Comments if the '-ENV' option is set. The 'use' is encapsulated in a BEGIN block triggered by the environmental variable for non-believers. Setting the variable $ENV{Smart_Comments} will load and turn on smart comment reporting. There are three levels of 'Smartness' available in this module '### #### #####'.
SUPPORT
TODO
AUTHOR
COPYRIGHT
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
Dependencies
SEE ALSO
- Smart::Comments - is used if the -ENV option is set
- Data::Walk
- Data::Walker
- Data::ModeMerge
2 POD Errors
The following errors were encountered while parsing the POD:
- Around line 422:
L<> starts or ends with whitespace
- Around line 532:
L<> starts or ends with whitespace