File Coverage

blib/lib/Data/Structure/Compare.pm
Criterion Covered Total %
statement 12 46 26.0
branch 0 10 0.0
condition n/a
subroutine 4 7 57.1
pod 0 2 0.0
total 16 65 24.6


line stmt bran cond sub pod time code
1             package Data::Structure::Compare;
2            
3 2     2   41842 use Exporter;
  2         4  
  2         154  
4             our @ISA = qw(Exporter);
5             our @EXPORT_OK = qw(hashes_to_hash structure_compare);
6            
7 2     2   12 use strict;
  2         3  
  2         62  
8 2     2   8 use warnings;
  2         8  
  2         56  
9 2     2   40 use 5.006;
  2         6  
  2         889  
10            
11             =head1 NAME
12            
13             Data::Structure::Compare : Compare the structure of two Hash reference
14            
15             =head1 VERSION
16            
17             Version 0.02
18            
19             =cut
20            
21             our $VERSION = '0.02';
22             # ----------------------------------
23             # compare hash, hash must is reference
24             #
25             sub hash_compare {
26 0     0 0   my ($hashes_1, $hashes_2) = @_;
27 0           my $hash_1 = hashes_to_hash($hashes_1);
28 0           say Dump($hash_1);
29 0           my $hash_2 = hashes_to_hash($hashes_2);
30 0           foreach my $key (keys %{$hash_1}) {
  0            
31 0 0         return 0 unless (exists $hash_2->{$key});
32             }
33 0           foreach my $key (keys %{$hash_2}) {
  0            
34 0 0         return 0 unless (exists $hash_1->{$key});
35             }
36 0           return 1;
37             }
38            
39             # ------------------------
40             # in default would parse depth 100 structure
41             #
42             sub hashes_to_hash {
43 0     0 0   my ($hash) = @_;
44 0           my $max_depth = 100;
45 0           my $flag = 0;
46 0           foreach (1 .. $max_depth) {
47 0           ($hash, $flag) = _transfer_hash($hash);
48 0 0         last if ($flag == 0);
49             }
50 0           return $hash;
51             }
52            
53             sub _transfer_hash {
54 0     0     my $ref_hashes = shift;
55 0           my $ref_hash = {};
56 0           my $expand_flag = 0;
57 0           my $split_char = "\x{ff}";
58 0           foreach my $key (keys %{$ref_hashes}) {
  0            
59 0           my $value = $ref_hashes->{$key};
60 0 0         if (ref($value) eq ref({})) {
61 0           foreach my $sub_key (keys %{$value}) {
  0            
62 0           my $sub_value = $value->{$sub_key};
63 0           $ref_hash->{"$key$split_char$sub_key"} = $sub_value;
64 0 0         $expand_flag++ if (ref($sub_value) eq ref({}));
65             }
66 0           next;
67             }
68 0           $ref_hash->{$key} = $value;
69             }
70 0           return ($ref_hash, $expand_flag);
71             }
72             =head1 SYNOPSIS
73            
74             use Data::Structure::Compare qw(structure_compare);
75            
76             my $data1 = {
77             key1 => 1,
78             key2 => 2,
79             key3 => {
80             key4 => 3,
81             key5 => {
82             key6 => 4,
83             },
84             },
85             };
86            
87             my $data1 = {
88             key1 => 11,
89             key2 => 12,
90             key3 => {
91             key4 => 13,
92             key5 => {
93             key6 => 14,
94             },
95             },
96             };
97            
98             if (structure_compare($data1, $data2) == 1) {
99             print '$data1 and $data2 have same structure';
100             }
101             ...
102            
103             =head1 EXPORT
104            
105             structure_compare : compare two Hash reference, Only compare the key name
106             In default, This module could compare the max depth is 100. It would not
107             compare the value with Array reference.
108            
109             =head1 AUTHOR
110            
111             Micheal Song, C<< >>
112            
113             =head1 BUGS
114            
115             Please report any bugs or feature requests to C, or through
116             the web interface at L. I will be notified, and then you'll
117             automatically be notified of progress on your bug as I make changes.
118            
119             =head1 SUPPORT
120            
121             You can find documentation for this module with the perldoc command.
122            
123             perldoc Data::Structure::Compare
124            
125            
126             You can also look for information at:
127            
128             =over 4
129            
130             =item * RT: CPAN's request tracker (report bugs here)
131            
132             L
133            
134             =item * AnnoCPAN: Annotated CPAN documentation
135            
136             L
137            
138             =item * CPAN Ratings
139            
140             L
141            
142             =item * Search CPAN
143            
144             L
145            
146             =back
147            
148            
149             =head1 ACKNOWLEDGEMENTS
150            
151            
152             =head1 LICENSE AND COPYRIGHT
153            
154             Copyright 2012 Micheal Song.
155            
156             This program is free software; you can redistribute it and/or modify it
157             under the terms of either: the GNU General Public License as published
158             by the Free Software Foundation; or the Artistic License.
159            
160             See http://dev.perl.org/licenses/ for more information.
161            
162            
163             =cut
164             1;