line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hub::Data::Create; |
2
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
34
|
|
3
|
1
|
|
|
1
|
|
5
|
use Hub qw/:lib/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
4
|
|
|
|
|
|
|
our $VERSION = '4.00043'; |
5
|
|
|
|
|
|
|
our @EXPORT = qw//; |
6
|
|
|
|
|
|
|
our @EXPORT_OK = qw/subset mkhandler resolve get_save_handler save_data/; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
9
|
|
|
|
|
|
|
# subset - Get a subset of hash values |
10
|
|
|
|
|
|
|
# subset - \%data, $regex |
11
|
|
|
|
|
|
|
# subset - \%data, $non-regex |
12
|
|
|
|
|
|
|
# In the second form (only one key can exist) the matching value is returned. |
13
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub subset { |
16
|
0
|
|
|
0
|
1
|
0
|
my $matches = undef; |
17
|
0
|
0
|
|
|
|
0
|
if ($_[1] =~ /^\{(.+)\}$/) { |
18
|
0
|
|
|
|
|
0
|
my $criteria = $1; |
19
|
0
|
|
|
|
|
0
|
my @expr = $criteria =~ /(\w+)\s+(.+)\s+(.+)/; |
20
|
0
|
0
|
|
|
|
0
|
if (@expr) { |
21
|
0
|
0
|
|
|
|
0
|
@$matches = grep { |
22
|
0
|
|
|
|
|
0
|
isa($_, 'HASH') and |
23
|
|
|
|
|
|
|
Hub::compare($expr[1], $_->{$expr[0]}, $expr[2]) |
24
|
|
|
|
|
|
|
} isa($_[0], 'ARRAY') |
25
|
0
|
|
|
|
|
0
|
? @{$_[0]} |
26
|
|
|
|
|
|
|
: isa($_[0], 'HASH') |
27
|
0
|
0
|
|
|
|
0
|
? values %{$_[0]} |
|
|
0
|
|
|
|
|
|
28
|
|
|
|
|
|
|
: (); |
29
|
|
|
|
|
|
|
} else { |
30
|
0
|
|
|
|
|
0
|
@$matches = isa($_[0], 'HASH') |
31
|
0
|
|
|
|
|
0
|
? map {$_[0]->{$_}} grep {/$criteria/} keys %{$_[0]} |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
32
|
|
|
|
|
|
|
: isa($_[0], 'ARRAY') |
33
|
0
|
0
|
|
|
|
0
|
? grep {/$criteria/} @{$_[0]} |
|
0
|
0
|
|
|
|
0
|
|
34
|
|
|
|
|
|
|
: undef; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
} else { |
37
|
0
|
0
|
|
|
|
0
|
return $_[0]->{$_[1]} if isa($_[0], 'HASH'); |
38
|
0
|
0
|
|
|
|
0
|
if (isa($_[0], 'ARRAY')) { |
39
|
0
|
|
|
|
|
0
|
my $key = $_[1]; |
40
|
0
|
0
|
|
|
|
0
|
@$matches = map {$$_{$key}} |
|
0
|
|
|
|
|
0
|
|
41
|
0
|
|
|
|
|
0
|
grep {isa($_, 'HASH') and exists $$_{$key}} @{$_[0]} |
|
0
|
|
|
|
|
0
|
|
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
0
|
0
|
|
|
|
0
|
return defined $matches |
|
|
0
|
|
|
|
|
|
45
|
|
|
|
|
|
|
? @$matches > 1 |
46
|
|
|
|
|
|
|
? mkinst('Subset', @$matches) |
47
|
|
|
|
|
|
|
: pop @$matches |
48
|
|
|
|
|
|
|
: undef; |
49
|
|
|
|
|
|
|
}#subset |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
52
|
|
|
|
|
|
|
# mkhandler - Get the parser for a given path |
53
|
|
|
|
|
|
|
# mkhandler $path |
54
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
55
|
|
|
|
|
|
|
#|test(regex) Hub::mkhandler('/jonnyboy.dat') |
56
|
|
|
|
|
|
|
#~Hub::Data::HashFile |
57
|
|
|
|
|
|
|
#|test(regex) Hub::mkhandler('/jonnyboy.data') |
58
|
|
|
|
|
|
|
#~Hub::Data::HashFile |
59
|
|
|
|
|
|
|
#|test(regex) Hub::mkhandler('/jonnyboy.hf') |
60
|
|
|
|
|
|
|
#~Hub::Data::HashFile |
61
|
|
|
|
|
|
|
#|test(regex) Hub::mkhandler('/data.dat.foo') |
62
|
|
|
|
|
|
|
#~Hub::Data::File |
63
|
|
|
|
|
|
|
#|test(regex) use Cwd qw(cwd); Hub::mkhandler(cwd()) |
64
|
|
|
|
|
|
|
#~Hub::Data::Directory |
65
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub mkhandler { |
68
|
16
|
|
|
16
|
1
|
18
|
my $parser = undef; |
69
|
16
|
100
|
|
|
|
149
|
if (-d $_[0]) { |
70
|
8
|
|
|
|
|
18
|
$parser = 'Directory'; |
71
|
|
|
|
|
|
|
} else { |
72
|
8
|
|
|
|
|
12
|
my $hf_types = '(\.hf|\.data?|'.Hub::META_FILENAME.')$'; |
73
|
8
|
50
|
|
|
|
115
|
$parser = $_[0] =~ /$hf_types/ ? 'HashFile' : 'File'; |
74
|
|
|
|
|
|
|
} |
75
|
16
|
50
|
|
|
|
40
|
confess "Cannot determine parser" unless defined $parser; |
76
|
16
|
|
|
|
|
63
|
Hub::mkinst($parser, $_[0]); |
77
|
|
|
|
|
|
|
}#mkhandler |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
80
|
|
|
|
|
|
|
# resolve - Get a string value for an object |
81
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub resolve { |
84
|
0
|
|
|
|
|
|
can($_[0], 'get_content') |
85
|
|
|
|
|
|
|
? $_[0]->get_content() |
86
|
|
|
|
|
|
|
: can($_[0], 'populate') |
87
|
0
|
|
|
|
|
|
? ${$_[0]->populate()} |
88
|
|
|
|
|
|
|
: ref($_[0]) eq 'SCALAR' |
89
|
0
|
0
|
|
0
|
1
|
|
? ${$_[0]} |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
90
|
|
|
|
|
|
|
: $_[0]; |
91
|
|
|
|
|
|
|
}#resolve |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
94
|
|
|
|
|
|
|
# get_save_handler - Save a node object (traverse upwards when needed) |
95
|
|
|
|
|
|
|
# get_save_hander $address, [%options] |
96
|
|
|
|
|
|
|
# |
97
|
|
|
|
|
|
|
# options: |
98
|
|
|
|
|
|
|
# |
99
|
|
|
|
|
|
|
# -as_addr=1 # Just return the address of the handler |
100
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub get_save_handler { |
103
|
0
|
|
|
0
|
1
|
|
my ($opts,$path) = Hub::opts(\@_, {as_addr=>0}); |
104
|
|
|
|
|
|
|
# my $path = shift; |
105
|
0
|
|
|
|
|
|
my $handler = (); |
106
|
|
|
|
|
|
|
# Find the handler for this address |
107
|
0
|
|
|
|
|
|
while (!defined $handler) { |
108
|
0
|
|
|
|
|
|
my $init = $$Hub{$path}; # Ensure it is loaded |
109
|
0
|
|
|
|
|
|
for (Hub::fhandler(Hub::realpath($path))) { |
110
|
0
|
0
|
|
|
|
|
if (UNIVERSAL::can($_, 'save')) { |
111
|
0
|
|
|
|
|
|
$handler = $_; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
0
|
0
|
|
|
|
|
unless (defined $handler) { |
115
|
0
|
|
|
|
|
|
$path = Hub::varparent($path); |
116
|
0
|
0
|
|
|
|
|
last unless $path; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
0
|
0
|
|
|
|
|
return $$opts{'as_addr'} ? $path : $handler; |
120
|
|
|
|
|
|
|
}#get_save_handler |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
123
|
|
|
|
|
|
|
# save_data - Save registry data |
124
|
|
|
|
|
|
|
# save_data $address |
125
|
|
|
|
|
|
|
# |
126
|
|
|
|
|
|
|
# Returns -1 if a handler cannot be found. |
127
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub save_data { |
130
|
0
|
|
|
0
|
1
|
|
my ($opts, $address) = Hub::opts(\@_); |
131
|
0
|
|
|
|
|
|
my $handler = Hub::get_save_handler($address); |
132
|
0
|
0
|
|
|
|
|
unless (defined $handler) { |
133
|
0
|
|
|
|
|
|
warn "Save handler not found for: $address\n"; |
134
|
0
|
|
|
|
|
|
return -1; |
135
|
|
|
|
|
|
|
} |
136
|
0
|
|
|
|
|
|
$handler->save(); |
137
|
|
|
|
|
|
|
}#save_data |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
1; |