line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Object::FromData::Hash; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
9
|
use strict; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
62
|
|
4
|
3
|
|
|
3
|
|
7
|
use warnings; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
55
|
|
5
|
3
|
|
|
3
|
|
7
|
use Digest::MD5 'md5_hex'; # convenience, not security |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
394
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub _new { |
10
|
3
|
|
|
3
|
|
3
|
my ( $class, $arg_for ) = @_; |
11
|
|
|
|
|
|
|
|
12
|
3
|
|
|
|
|
3
|
my $hashref = $arg_for->{ref}; |
13
|
3
|
|
50
|
|
|
12
|
my $allow_private = $arg_for->{allow_private} || 0; |
14
|
3
|
|
|
|
|
13
|
my @all_keys = sort( CORE::keys(%$hashref) ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $self = { |
17
|
|
|
|
|
|
|
hash => {}, |
18
|
|
|
|
|
|
|
allow_private => $allow_private, |
19
|
3
|
50
|
|
|
|
5
|
keys => [ $allow_private ? @all_keys : grep { !/^_/ } @all_keys ], |
|
9
|
|
|
|
|
18
|
|
20
|
|
|
|
|
|
|
}; |
21
|
3
|
|
|
|
|
5
|
my $new_class = "${class}::" . md5_hex( join '-' => @{ $self->{keys} } ); |
|
3
|
|
|
|
|
13
|
|
22
|
|
|
|
|
|
|
{ |
23
|
3
|
|
|
3
|
|
18
|
no strict 'refs'; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
357
|
|
|
3
|
|
|
|
|
3
|
|
24
|
3
|
|
|
|
|
2
|
@{"${new_class}::ISA"} = $class; |
|
3
|
|
|
|
|
30
|
|
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
3
|
|
|
|
|
4
|
KEY: foreach my $key (@all_keys) { |
28
|
9
|
|
|
|
|
10
|
my $value = $hashref->{$key}; |
29
|
9
|
100
|
|
|
|
23
|
if ( 'ARRAY' eq ref $value ) { |
|
|
100
|
|
|
|
|
|
30
|
1
|
|
|
|
|
6
|
$self->{hash}{$key} |
31
|
|
|
|
|
|
|
= Object::FromData::Array->_new( { ref => $value } ); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
elsif ( 'HASH' eq ref $value ) { |
34
|
1
|
|
|
|
|
4
|
$self->{hash}{$key} = $class->_new( { ref => $value } ); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
else { |
37
|
7
|
|
|
|
|
9
|
$self->{hash}{$key} = $value; |
38
|
|
|
|
|
|
|
} |
39
|
9
|
100
|
|
|
|
16
|
if ( $key =~ /^_/ ) { |
40
|
1
|
50
|
|
|
|
3
|
next KEY unless $allow_private; |
41
|
|
|
|
|
|
|
} |
42
|
3
|
|
|
3
|
|
10
|
no strict 'refs'; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
525
|
|
43
|
8
|
|
|
5
|
|
14
|
*{"${new_class}::$key"} = sub { $self->{hash}{$key} }; |
|
8
|
|
|
|
|
27
|
|
|
5
|
|
|
|
|
25
|
|
44
|
|
|
|
|
|
|
} |
45
|
3
|
|
|
|
|
10
|
return bless $self => $new_class; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub keys { |
49
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
50
|
1
|
50
|
|
|
|
3
|
$self = shift if @_; |
51
|
1
|
|
|
|
|
2
|
return keys %{ $self->{hash} }; |
|
1
|
|
|
|
|
8
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub values { |
55
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
56
|
0
|
0
|
|
|
|
|
$self = shift if @_; |
57
|
0
|
|
|
|
|
|
return values %{ $self->{hash} }; |
|
0
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
0
|
1
|
|
sub is_hashref {1} |
61
|
|
|
|
0
|
1
|
|
sub is_arrayref { } |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
1; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
__END__ |