Config::Hierarchical::Delta - Comparator for hashes and Config::Hierarchical objects


Config-Hierarchical documentation Contained in the Config-Hierarchical distribution.

Index


Code Index:

NAME

Top

 Config::Hierarchical::Delta - Comparator for hashes and Config::Hierarchical objects

SYNOPSIS

Top

	# comparing hashes:

	use Config::Hierarchical ; 
	use Config::Hierarchical::Delta qw (GetConfigDelta GetConfigHierarchicalDelta DumpConfigHierarchicalDelta Get_NoIdentical_Filter) ; 

	my $delta = GetConfigDelta
				(
				{name   => {A => 1, COMMON => 0}},
				{name_2 => {B => 2, COMMON => 0}}
				) ;

$delta is a reference to the following hash:

	{
	'in \'name\' only'   => {'A' => 1},
	'in \'name_2\' only' => {'B' => 2},
	'identical'          => {'COMMON' => 0},
	},

	# comparing Config Hierarchical objects:

	my $config_0 = new Config::Hierarchical
				(
				NAME => 'config 0',
				INITIAL_VALUES  =>
					[
					{NAME => 'CC1', VALUE => '1'},
					{NAME => 'CC2', VALUE => '2'},
					] ,
				) ;

	my $config_1 = new Config::Hierarchical
				(
				NAME => 'config 1',
				CATEGORY_NAMES   => ['A', 'B',],
				DEFAULT_CATEGORY => 'A',

				INITIAL_VALUES  =>
					[
					{CATEGORY => 'B', ALIAS => $config_0},

					{NAME => 'CC1', VALUE => '1'},
					{NAME => 'CC2', VALUE => '2'},
					{NAME => 'CC3', VALUE => '3'},
					] ,
				) ;

	$config_1->Set(NAME => 'CC1', VALUE => '1.1') ;

	my $config_2 = new Config::Hierarchical
				(
				NAME => 'config 2',

				CATEGORY_NAMES   => ['<A>', 'B',],
				DEFAULT_CATEGORY => 'A',
				INITIAL_VALUES   =>
					[
					{CATEGORY => 'B', ALIAS => $config_1},
					] ,
				) ;

	$config_2->Set(CATEGORY => 'A', NAME => 'CC1', VALUE => 'A', OVERRIDE => 1) ;
	$config_2->Set(CATEGORY => 'A', NAME => 'XYZ', VALUE => 'xyz') ;

	my $dump = DumpConfigHierarchicalDelta($config_2, $config_0) ;

$dump contains the following string:

	Delta between 'config 2' and 'config 0'':
	|- different 
	|  `- CC1 
	|     |- config 0 = 1 
	|     `- config 2 = A 
	|- identical 
	|  `- CC2 = 2 
	`- in 'config 2' only 
	   |- CC3 = 3 
	   `- XYZ = xyz 

DESCRIPTION

Top

This module lets you compare hashes and Config::Hierarchical objects.

DOCUMENTATION

Top

SUBROUTINES/METHODS

Top

GetConfigDelta

	my $delta = GetConfigDelta
				(
				{name   => {A => 1, COMMON => 0}},
				{name_2 => {B => 2, COMMON => 0}}
				) ;

GetConfigDelta compares two hashes and returns a reference to a hash containing up to 4 elements. It takes as argument two hash reference which contain a single element. The key is used as name for the hash while the value is a reference to the hash to be compared.

Returned elements:

* identical

Contains all the elements that are identical in both hashes as well as the value they have

* different

Contains the elements that are common in both hashes but with different values

* in 'lhs' only

Contains the elements that exists in the first hash but not in the second hash .

* in 'rhs' only

Contains the elements that exists in the first hash but not in the second hash .

GetConfigHierarchicalDelta

	my $config_1 = new Config::Hierarchical(...) ;
	my $config_2 = new Config::Hierarchical(...) ;

	GetConfigHierarchicalDelta($config_1, $config_2) ;

Compares two Config::Hierarchical objects and returns a reference to hash containing the delta between the objects. See GetConfigDeleta for a description of the returned hash.

The name of the Config::Variable object is extracted from the objects.

DumpConfigHierarchicalDelta

	my $config_1 = new Config::Hierarchical(...)
	my $config_2 = new Config::Hierarchical(...) ;

	print DumpConfigHierarchicalDelta($config_1, $config_2, QUOTE_VALUES => 1) ;




The first two arguments a Config::Hierarchical objects, the rest of the arguments are passed as is to Data::TreeDumper.

This sub returns a string containing the dump of the delta. See Synopsis for an output example.

Get_NoIdentical_Filter

Dumping a config delta with:

	print  DumpConfigHierarchicalDelta($config_2, $config_0) ;	

Gives:

	$expected_dump = <<EOD ;
	Delta between 'config 2' and 'config 0'':
	|- different 
	|  `- CC1 
	|     |- config 0 = 1 
	|     `- config 2 = A 
	|- identical 
	|  `- CC2 = 2 
	`- in 'config 2' only 
	   |- CC3 = 3 
	   `- XYZ = xyz 




if you do not want to display the configuration variables that are identical, use:

	print  DumpConfigHierarchicalDelta($config_2, $config_0, Get_NoIdentical_Filter()) ;

which gives:

	my $expected_dump = <<EOD ;
	Delta between 'config 2' and 'config 0'':
	|- different 
	|  `- CC1 
	|     |- config 0 = 1 
	|     `- config 2 = A 
	`- in 'config 2' only 
	   |- CC3 = 3 
	   `- XYZ = xyz 
	EOD

Returns a Data::TreeDumper filter you can use to remove the 'identical' element from the delta hash.

BUGS AND LIMITATIONS

Top

None so far.

AUTHOR

Top

	Khemir Nadim ibn Hamouda
	CPAN ID: NKH
	mailto:nadim@khemir.net

LICENSE AND COPYRIGHT

Top

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Config::Hierarchical

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Config-Hierarchical

* RT: CPAN's request tracker

Please report any bugs or feature requests to L <bug-config-hierarchical@rt.cpan.org>.

We will be notified, and then you'll automatically be notified of progress on your bug as we make changes.

* Search CPAN

http://search.cpan.org/dist/Config-Hierarchical

SEE ALSO

Top


Config-Hierarchical documentation Contained in the Config-Hierarchical distribution.
package Config::Hierarchical::Delta ;
use base Exporter ;

use strict;
use warnings ;

BEGIN 
{
use Exporter ();

use vars qw ($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);

$VERSION     = 0.01;
@EXPORT_OK   = qw (GetConfigDelta GetConfigHierarchicalDelta DumpConfigHierarchicalDelta Get_NoIdentical_Filter);
%EXPORT_TAGS = ();
}

#-------------------------------------------------------------------------------

use Carp ;
use Data::Compare ;
use Data::TreeDumper ;
use Sub::Install ;

use English qw( -no_match_vars ) ;

use Readonly ;
Readonly my $EMPTY_STRING => q{} ;

#-------------------------------------------------------------------------------

sub GetConfigDelta
{

my ($lhs,  $rhs) = @_ ;

die "GetConfigDelta: Error, wrong argument type on the left hand side, expected hash with a single element!\n" unless 'HASH' eq ref $lhs ;
die "GetConfigDelta: Error, wrong argument type on the right hand side, expected hash with a single element!\n" unless 'HASH' eq ref $rhs ;

die "GetConfigDelta: Error, only one element expected on left hand side\n" unless 1 == scalar(keys %{$lhs}) ;
die "GetConfigDelta: Error, only one element expected on right hand side\n" unless 1 == scalar(keys %{$rhs}) ;

my $lhs_name = (keys %{$lhs})[0] ;
my $rhs_name = (keys %{$rhs})[0] ;

die "GetConfigDelta: Error, expected a HASH as a config on the left hand side\n" unless 'HASH' eq ref $lhs->{$lhs_name} ;
die "GetConfigDelta: Error, expected a HASH as a config on the right hand side\n" unless 'HASH' eq ref $rhs->{$rhs_name} ;

# make lhs and rhs point to the configs
($lhs,  $rhs) = ($lhs->{$lhs_name}, $rhs->{$rhs_name}) ;

my %delta ;

for my $key( keys %{$lhs})
	{
	if(exists $rhs->{$key})
		{
		if(!Compare($rhs->{$key}, $lhs->{$key}))
			{
			$delta{different}{$key} = {$lhs_name => $lhs->{$key}, $rhs_name => $rhs->{$key}}
			}
		else
			{
			$delta{identical}{$key} = $lhs->{$key} ;
			}
		}
	else
		{
		$delta{"in '$lhs_name' only"}{$key} = $lhs->{$key} ;
		}
	}
	
for my $key( keys %{$rhs})
	{
	unless(exists $lhs->{$key})
		{
		$delta{"in '$rhs_name' only"}{$key} = $rhs->{$key} ;
		}
	}

return(\%delta) ;
}
  
#-------------------------------------------------------------------------------

sub GetConfigHierarchicalDelta
{


my ($lhs,  $rhs) = @_ ;

die "GetConfigHierarchicalDelta: Error, expected a 'Config::Hierarchical' on the left hand side\n" unless 'Config::Hierarchical' eq ref $lhs ;
die "GetConfigHierarchicalDelta: Error, expected a 'Config::Hierarchical' on the right hand side\n" unless 'Config::Hierarchical' eq ref $rhs ;

my ($lhs_name) = $lhs->GetInformation() ;
my ($rhs_name) = $rhs->GetInformation() ;

my $lhs_hash_ref = $lhs->GetHashRef() ;
my $rhs_hash_ref = $rhs->GetHashRef() ;

return( GetConfigDelta({$lhs_name=> $lhs_hash_ref} ,{$rhs_name=> $rhs_hash_ref}) );
}

#-------------------------------------------------------------------------------

sub DumpConfigHierarchicalDelta
{

my ($lhs,  $rhs, @other_arguments_to_data_treedumper) = @_ ;

die "GetConfigHierarchicalDelta: Error, expected a 'Config::Hierarchical' on the left hand side\n" unless 'Config::Hierarchical' eq ref $lhs ;
die "GetConfigHierarchicalDelta: Error, expected a 'Config::Hierarchical' on the right hand side\n" unless 'Config::Hierarchical' eq ref $rhs ;

my ($lhs_name) = $lhs->GetInformation() ;
my ($rhs_name) = $rhs->GetInformation() ;

return
	(
	DumpTree GetConfigHierarchicalDelta($lhs ,$rhs) ,
	"Delta between '$lhs_name' and '$rhs_name'':", 
	DISPLAY_ADDRESS => 0,
	@other_arguments_to_data_treedumper 
	) ;
}

#-------------------------------------------------------------------------------

sub Get_NoIdentical_Filter
{

return (LEVEL_FILTERS => {0 => sub {my $s = shift ; return('HASH', undef, grep {$_ ne 'identical'} keys %{$s})}}) ;
}

#-------------------------------------------------------------------------------

1 ;