NAME
Data::Walk::Clone - deep data cloning with boundaries
SYNOPSIS
#!perl
use Modern::Perl;
use Moose::Util qw( with_traits );
use Data::Walk::Extracted v0.011;
use Data::Walk::Clone v0.003;
my $dr_nisar_ahmad_wani = with_traits(
'Data::Walk::Extracted',
( 'Data::Walk::Clone', )
)->new(
skip_clone_tests =>[ [ 'HASH', 'LowerKey2', 'ALL', 'ALL' ] ],
);
my $donor_ref = {
Someotherkey => 'value',
Parsing =>{
HashRef =>{
LOGGER =>{
run => 'INFO',
},
},
},
Helping =>[
'Somelevel',
{
MyKey =>{
MiddleKey =>{
LowerKey1 => 'lvalue1',
LowerKey2 => {
BottomKey1 => 'bvalue1',
BottomKey2 => 'bvalue2',
},
},
},
},
],
};
my $injaz_ref = $dr_nisar_ahmad_wani->deep_clone(
donor_ref => $donor_ref,
);
if(
$injaz_ref->{Helping}->[1]->{MyKey}->{MiddleKey}->{LowerKey2} eq
$donor_ref->{Helping}->[1]->{MyKey}->{MiddleKey}->{LowerKey2} ){
print "The data is not cloned at the skip point\n";
}
if(
$injaz_ref->{Helping}->[1]->{MyKey}->{MiddleKey} ne
$donor_ref->{Helping}->[1]->{MyKey}->{MiddleKey} ){
print "The data is cloned above the skip point\n";
}
#####################################################################################
# Output of SYNOPSIS
# 01 The data is not cloned at the skip point
# 02 The data is cloned above the skip point
#####################################################################################
DESCRIPTION
This Moose::Role contains methods for implementing the method deep_clone using Data::Walk::Extracted. This method is used to deep clone a data ref. Cloning is accomplished by sending a 'donor_ref' that has data that you want copied into a different memory location. In general Data::Walk::Extracted already deep clones any output as part of its data walking so the primary value of this Role is to define deep cloning boundaries. It may be that some portion of the data should maintain common memory location pointers to the original memory locations and so two ways of defining where to stop deep cloning are provided. First a level callout where deep cloning can stop at a common level. Second a matching tool where key or node type matching can be done across multiple levels or only at targeted levels.
Caveat utilitor
Supported Node types
- ARRAY
- HASH
- SCALAR
Supported one shot "Attributes"
- clone_level
- skip_clone_tests
- should_clone
USE
One way to join this role with Data::Walk::Extracted is the method 'with_traits' from Moose::Util. Otherwise see Moose::Manual::Roles.
Methods
deep_clone( %args )
- Definition: This takes a 'donor_ref' and deep clones it.
- Accepts: This method accepts data in three ways. A list, a hashref, or a single data reference. If a list is passed then it is assumed to be a fat comma list and is passed to _process_the_data in Data::Walk::Extracted as a hash ref. If a hashref is passed then it is checked for a 'donor_ref' key and then passed to '_process_the_data' directly if the key exists. If the key doesnt exist or the single passed value is not a hashref then then the data is added as a value to the key donor_ref and passed through as a hashref to '_process_the_data'. The donor_ref can contain any of the accepted data nodes. Some attributes can also be changed for the duration of this method by using the attribute name as a key in %args. See Data::Walk::Extracted Attributes for more explanation.
- Returns: The deep cloned data reference
has_clone_level
- Definition: This will indicate if the attribute clone_level is set
- Accepts: nothing
- Returns: 1 or 0
get_clone_level
- Definition: This will return the currently set clone_level attribute value
- Accepts: nothing
- Returns: the level as an integer number or undef for nothing
set_clone_level
- Definition: This will set the clone_level attribute
- Accepts: a positive integer
- Returns: nothing
clear_clone_level
- Definition: This will clear the clone_level attribute
- Accepts: nothing
- Returns: nothing
get_skip_clone_tests
- Definition: This will return an ArrayRef[ArrayRef] with all skip tests for the skip_clone_tests attribute
- Accepts: nothing
- Returns: an ArrayRef[ArrayRef]
set_skip_clone_tests
- Definition: This will take an ArrayRef[ArrayRef] with all skip tests for the skip_clone_tests attribute and replace any existing tests with the new list.
- Accepts: ArrayRef[ArrayRef]
- Returns: nothing
add_skip_clone_test
- Definition: This will add one array ref skip test callout to the skip_clone_tests attribute list
- Accepts: [ $type, $key, $position, $level ]
- Returns: nothing
has_skip_clone_tests
- Definition: This will return the number of skip tests called out in the skip_clone_tests attribute list
- Accepts: nothing
- Returns: a positive integer indicating how many array positions there are
clear_skip_clone_tests
- Definition: This will clear the the skip_clone_tests attribute list
- Accepts: nothing
- Returns: nothing
get_should_clone
- Definition: This will get the current value of the attribute should_clone
- Accepts: nothing
- Returns: a boolean value
set_should_clone( $Bool )
- Definition: This will set the attribute should_clone
- Accepts: a boolean value
- Returns: nothing
has_should_clone
- Definition: This will return true if the attribute should_clone is active
- Accepts: nothing
- Returns: a boolean value
clear_should_clone
- Definition: This will set the attribute should_clone to on ( 1 ). The name is awkward to accomodate one shot attribute changes.
- Accepts: nothing
- Returns: nothing
Attributes
Data passed to ->new when creating an instance. 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 meet the criteria can be passed to deep_clone and will be adjusted for just that run of the method.
clone_level
- Definition: When running a clone operation it is possible to stop cloning and use the actual data references in the original data structure below a certain level. This sets the boundary for that level.
- Default undefined = everything is cloned
- Range positive integers (3 means clone to the 3rd level)
skip_clone_tests
- Definition: When running a clone operation it is possible to stop cloning and use the actual data references in the original data structure at clearly defined trigger points. This is the way to define those points. The definition can test against array position, match a hash key, also only test at a level (Testing at level 0 is not supported!), and can use eq or =~ when matching. The attribute is passed an ArrayRef of ArrayRefs. Each sub_ref contains the following.
-
- $type - this is any of the allowed reference node types
- $key - this is either a scalar or regexref to use for matching a hash key
- $position - this is used to match an array position can be an integer or 'ANY'
- $level - this restricts the skipping test usage to a specific level only or 'ANY'
example: [ [ 'HASH', 'KeyWord', 'ANY', 'ANY'], # Dont clone the value of any hash key eq 'Keyword' [ 'ARRAY', 'ANY', '3', '4'], ], #Don't clone the data in arrays at position three on level four ]
- Range an infinite number of skip tests added to an array
- Default [] = no cloning is skipped
should_clone
- Definition: There are times when the cloning is built into code by adding the Role to a class but you want to turn it off. This attribute will cause the deep_clone function to return the donor_ref pointer as the cloned_ref. This can be set for each run.
- Default 1 = cloning occurs
- Range 1 | 0 = no cloning
See also
Attributes in Data::Walk::Extracted can 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
SEE ALSO
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 611:
L<> starts or ends with whitespace