line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hub::Data::File; |
2
|
1
|
|
|
1
|
|
8
|
use strict; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
47
|
|
3
|
1
|
|
|
1
|
|
5
|
use Hub qw/:lib/; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
6
|
|
4
|
|
|
|
|
|
|
our $AUTOLOAD = ''; |
5
|
|
|
|
|
|
|
our $VERSION = '4.00043'; |
6
|
|
|
|
|
|
|
our @EXPORT = qw//; |
7
|
|
|
|
|
|
|
our @EXPORT_OK = qw//; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
10
|
|
|
|
|
|
|
# new - Constructor. |
11
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
8
|
|
|
8
|
1
|
18
|
my $self = shift; |
15
|
8
|
|
33
|
|
|
44
|
my $classname = ref( $self ) || $self; |
16
|
8
|
|
33
|
|
|
22
|
my $filename = shift || croak "Provide a file name"; |
17
|
8
|
|
|
|
|
34
|
my $obj = Hub::fhandler($filename, $classname); |
18
|
8
|
50
|
|
|
|
17
|
unless($obj) { |
19
|
8
|
|
|
|
|
30
|
$obj = bless {}, $classname; |
20
|
8
|
|
|
|
|
72
|
tie %$obj, 'Hub::Knots::TiedObject', 'Hub::Knots::SortedHash'; |
21
|
8
|
|
|
|
|
39
|
$obj->{'*filename'} = $filename; |
22
|
8
|
|
|
|
|
36
|
$obj->{'*contents'} = undef; |
23
|
8
|
|
|
|
|
36
|
$obj->{'*data'} = undef; |
24
|
8
|
|
|
|
|
36
|
$obj->{'*delay_reading'} = 0; |
25
|
8
|
|
|
|
|
37
|
$obj->{'*is_binary'} = -B $$obj{'*filename'}; |
26
|
8
|
|
|
|
|
36
|
Hub::fattach($filename, $obj); |
27
|
|
|
|
|
|
|
} |
28
|
8
|
|
|
|
|
64
|
return $obj; |
29
|
|
|
|
|
|
|
}#new |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
32
|
|
|
|
|
|
|
# delay_reading - Instruct L to delay reading from disk |
33
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub delay_reading { |
36
|
8
|
|
|
8
|
1
|
13
|
my $self = shift; |
37
|
8
|
50
|
|
|
|
18
|
croak "Illegal call to instance method" unless ref($self); |
38
|
8
|
|
|
|
|
38
|
return $$self{'*delay_reading'}; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
42
|
|
|
|
|
|
|
# reload - Callback from L when a read from disk is performed |
43
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub reload { |
46
|
8
|
|
|
8
|
1
|
38
|
my ($self,$opts,$file) = Hub::objopts(\@_); |
47
|
8
|
50
|
|
|
|
24
|
croak "Illegal call to instance method" unless ref($self); |
48
|
8
|
|
|
|
|
89
|
$self->{'*contents'} = \$file->{'contents'}; |
49
|
8
|
|
|
|
|
47
|
$self->{'*is_binary'} = -B $$self{'*filename'}; |
50
|
8
|
100
|
|
|
|
43
|
if (!$$self{'*is_binary'}) { |
51
|
7
|
|
|
|
|
41
|
for (keys %$self) { delete $self->{$_}; } |
|
0
|
|
|
|
|
0
|
|
52
|
7
|
|
|
|
|
50
|
my $extractor = Hub::mkinst('DataExtractor', |
53
|
|
|
|
|
|
|
-template => \$file->{'contents'}, -data => $self); |
54
|
7
|
|
|
|
|
39
|
$extractor->get_data(); |
55
|
|
|
|
|
|
|
} |
56
|
8
|
|
|
|
|
56
|
$$self{'*delay_reading'} = 0; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
60
|
|
|
|
|
|
|
# get_data - Get a reference to the hash data defined in this file |
61
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub get_data { |
64
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
65
|
0
|
|
|
|
|
|
my $addr = shift; |
66
|
0
|
0
|
|
|
|
|
croak "Illegal call to instance method" unless ref($self); |
67
|
0
|
0
|
|
|
|
|
if (!defined $$self{'*contents'}) { |
68
|
0
|
|
|
|
|
|
$$self{'*delay_reading'} = 0; |
69
|
0
|
|
|
|
|
|
my $instance = Hub::finstance($$self{'*filename'}); |
70
|
0
|
|
|
|
|
|
Hub::fread($instance); |
71
|
|
|
|
|
|
|
} |
72
|
0
|
0
|
|
|
|
|
return defined $addr ? Hub::subset($self, $addr) : $self; |
73
|
|
|
|
|
|
|
}#get_data |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
76
|
|
|
|
|
|
|
# get_content - Return file contents |
77
|
|
|
|
|
|
|
# get_content [options] |
78
|
|
|
|
|
|
|
# |
79
|
|
|
|
|
|
|
# options: |
80
|
|
|
|
|
|
|
# |
81
|
|
|
|
|
|
|
# -as_ref => 1 # Return a reference |
82
|
|
|
|
|
|
|
# -text_only => 1 # Do not return binary data |
83
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub get_content { |
86
|
0
|
|
|
0
|
1
|
|
my ($opts, $self) = Hub::opts(\@_, {'as_ref' => 0}); |
87
|
0
|
0
|
|
|
|
|
croak "Illegal call to instance method" unless ref($self); |
88
|
0
|
0
|
0
|
|
|
|
return if ($$opts{'text_only'} && $$self{'*is_binary'}); |
89
|
0
|
0
|
|
|
|
|
if (!defined $$self{'*contents'}) { |
90
|
0
|
|
|
|
|
|
$$self{'*delay_reading'} = 0; |
91
|
0
|
|
|
|
|
|
my $instance = Hub::finstance($$self{'*filename'}); |
92
|
0
|
|
|
|
|
|
Hub::fread($instance); |
93
|
|
|
|
|
|
|
} |
94
|
0
|
|
|
|
|
|
return $$opts{'as_ref'} |
95
|
|
|
|
|
|
|
? $$self{'*contents'} |
96
|
|
|
|
|
|
|
: defined $$self{'*contents'} |
97
|
0
|
0
|
|
|
|
|
? ${$$self{'*contents'}} |
|
|
0
|
|
|
|
|
|
98
|
|
|
|
|
|
|
: ''; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
102
|
|
|
|
|
|
|
# set_content - Set file contents |
103
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub set_content { |
106
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
107
|
0
|
|
|
|
|
|
my $contents = shift; |
108
|
0
|
0
|
|
|
|
|
croak "Illegal call to instance method" unless ref($self); |
109
|
0
|
0
|
|
|
|
|
$$self{'*contents'} = ref($contents) ? $contents : \$contents; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
113
|
|
|
|
|
|
|
# save - Save file contents to disk |
114
|
|
|
|
|
|
|
# save [options] |
115
|
|
|
|
|
|
|
# |
116
|
|
|
|
|
|
|
# options: |
117
|
|
|
|
|
|
|
# -priority => 'content' Content values presceed data values |
118
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub save { |
121
|
0
|
|
|
0
|
1
|
|
my ($opts,$self) = Hub::opts(\@_, {'priority' => 'data'}); |
122
|
0
|
0
|
|
|
|
|
croak "Illegal call to instance method" unless ref($self); |
123
|
0
|
0
|
|
|
|
|
if (defined $$self{'*contents'}) { |
124
|
0
|
0
|
|
|
|
|
$self->_merge_data_into_content() unless $$opts{'priority'} eq 'content'; |
125
|
0
|
|
|
|
|
|
Hub::writefile($$self{'*filename'}, $self->get_content()); |
126
|
0
|
|
|
|
|
|
$$self{'*delay_reading'} = 0; |
127
|
0
|
|
|
|
|
|
Hub::frefresh($$self{'*filename'}, -force); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub _merge_data_into_content { |
132
|
0
|
|
|
0
|
|
|
my ($opts, $self) = Hub::opts(\@_); |
133
|
0
|
|
|
|
|
|
my $injector = Hub::mkinst('DataInjector', -opts => $opts, |
134
|
|
|
|
|
|
|
-template => $self->get_content(-as_ref => 1)); |
135
|
0
|
|
|
|
|
|
$self->{'*contents'} = $injector->populate($self->get_data()); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub set_sort_order { |
140
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
141
|
0
|
0
|
|
|
|
|
croak "Illegal call to instance method" unless ref($self); |
142
|
0
|
|
|
|
|
|
my $sort_order = shift; |
143
|
0
|
0
|
|
|
|
|
croak "Provide an array reference" unless isa($sort_order, 'ARRAY'); |
144
|
0
|
|
|
|
|
|
$self->{'*tied'}->set_sort_keys(@$sort_order); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
148
|
|
|
|
|
|
|
1; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
__END__ |