NAME
Data::Walk::Prune - A way to say what should be removed
SYNOPSIS
#! C:/Perl/bin/perl
use Modern::Perl;
use Moose::Util qw( with_traits );
use Data::Walk::Extracted v0.011;
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 $result 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 will take a 'slice_ref' and use it to prune a 'tree_ref'. The code looks for empty hash refs or array refs to show where to cut. The cut is signaled by the location an empty hash ref or an empty node ref. If the terminator is the value in a key => value pair of a hash node then the key is also deleted. If the terminator is in a position in an array then the array position is deleted/cleared. If the slice ref terminator is not a match for a node on the tree ref then no cutting occurs!
- Accepts: a hash ref with the keys 'slice_ref' and 'tree_ref' (both required). The slice ref can contain more than one terminator location in the data reference.
- 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
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 with the '-ENV' option so setting the variable $ENV{Smart_Comments} will turn on smart comment reporting. There are three levels of 'Smartness' called in this module '### #### #####'. See the Smart::Comments documentation for more information.
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.
Dependancies
- Data::Walk::Extracted
- version
- Moose::Role
- MooseX::Types::Moose
- Smart::Comments - With the -ENV variable set