line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: Storable.pm 69 2014-05-23 10:27:30Z adam $ |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Config::Trivial::Storable; |
4
|
|
|
|
|
|
|
|
5
|
10
|
|
|
10
|
|
124877
|
use base qw( Config::Trivial ); |
|
10
|
|
|
|
|
23
|
|
|
10
|
|
|
|
|
11307
|
|
6
|
|
|
|
|
|
|
|
7
|
10
|
|
|
10
|
|
79566
|
use 5.010; |
|
10
|
|
|
|
|
37
|
|
|
10
|
|
|
|
|
434
|
|
8
|
10
|
|
|
10
|
|
66
|
use strict; |
|
10
|
|
|
|
|
25
|
|
|
10
|
|
|
|
|
334
|
|
9
|
10
|
|
|
10
|
|
55
|
use Carp; |
|
10
|
|
|
|
|
22
|
|
|
10
|
|
|
|
|
715
|
|
10
|
10
|
|
|
10
|
|
53
|
use warnings; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
308
|
|
11
|
10
|
|
|
10
|
|
17080
|
use Storable qw(lock_store lock_retrieve); |
|
10
|
|
|
|
|
63802
|
|
|
10
|
|
|
|
|
5884
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.32'; |
14
|
|
|
|
|
|
|
my ( $_package, $_file ) = caller; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
# STORE |
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub store { |
21
|
9
|
|
|
9
|
1
|
51864
|
my $self = shift; |
22
|
9
|
|
|
|
|
32
|
my %args = @_; |
23
|
|
|
|
|
|
|
|
24
|
9
|
|
66
|
|
|
84
|
my $file = $args{'config_file'} |
25
|
|
|
|
|
|
|
|| $self->{_storable_file} |
26
|
|
|
|
|
|
|
|| $self->{_config_file}; |
27
|
|
|
|
|
|
|
|
28
|
9
|
100
|
66
|
|
|
150
|
if ( ( ( $self->{_self} ) |
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
29
|
|
|
|
|
|
|
&& ( ( $file =~ '\(eval ' ) || ( $file =~ 'base.pm' ) ) ) |
30
|
|
|
|
|
|
|
|| ( $_file eq $file ) |
31
|
|
|
|
|
|
|
|| ( $0 eq $file ) ) |
32
|
|
|
|
|
|
|
{ |
33
|
4
|
|
|
|
|
18
|
return $self->_raise_error( |
34
|
|
|
|
|
|
|
'Not allowed to store to the calling file.'); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
5
|
100
|
|
|
|
70
|
if ( -e $file ) { |
38
|
1
|
50
|
|
|
|
17
|
croak "ERROR: Insufficient permissions to write to: $file" |
39
|
|
|
|
|
|
|
unless ( -w $file ); |
40
|
1
|
50
|
|
|
|
70
|
rename $file, $file . $self->{_backup_char} |
41
|
|
|
|
|
|
|
or croak "ERROR: Unable to rename $file."; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
5
|
|
100
|
|
|
25
|
my $settings = $args{'configuration'} || $self->{_configuration}; |
45
|
|
|
|
|
|
|
|
46
|
5
|
100
|
100
|
|
|
44
|
if ( ! ( $settings && ref $settings eq 'HASH') ) { |
47
|
2
|
|
|
|
|
7
|
return $self->_raise_error( |
48
|
|
|
|
|
|
|
q{Configuration object isn't a HASH reference.}) |
49
|
|
|
|
|
|
|
}; |
50
|
|
|
|
|
|
|
|
51
|
3
|
50
|
|
|
|
12
|
lock_store $settings, $file or croak "Unable to store to $file"; |
52
|
|
|
|
|
|
|
|
53
|
3
|
|
|
|
|
5109
|
return 1; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# |
57
|
|
|
|
|
|
|
# RETRIEVE |
58
|
|
|
|
|
|
|
# |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub retrieve { |
61
|
15
|
|
|
15
|
1
|
16813
|
my $self = shift; |
62
|
15
|
|
|
|
|
26
|
my $key = shift; # If there is a key, return only it's value |
63
|
15
|
|
|
|
|
157
|
my $retrieved_hash_ref; |
64
|
|
|
|
|
|
|
my $file; |
65
|
|
|
|
|
|
|
|
66
|
15
|
100
|
100
|
|
|
792
|
if ( $self->{_config_file} && $self->{_storable_file} ) { |
67
|
4
|
100
|
|
|
|
1190
|
if ( $self->{_config_file} eq $self->{_storable_file} ) { |
|
|
100
|
|
|
|
|
|
68
|
2
|
|
|
|
|
3
|
$file = $self->{_config_file}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
elsif ( ( stat $self->{_config_file} )[9] |
71
|
|
|
|
|
|
|
> ( stat $self->{_storable_file} )[9] ) |
72
|
|
|
|
|
|
|
{ |
73
|
1
|
50
|
|
|
|
7
|
return $self->read($key) if $key; |
74
|
0
|
|
|
|
|
0
|
return $self->read; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
else { |
77
|
1
|
|
|
|
|
4
|
$file = $self->{_storable_file}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
else { |
81
|
11
|
100
|
|
|
|
28
|
if ( $self->{_storable_file} ) { |
82
|
2
|
|
|
|
|
4
|
$file = $self->{_storable_file}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
else { |
85
|
10
|
|
|
10
|
|
91
|
no warnings qw( uninitialized ); |
|
10
|
|
|
|
|
24
|
|
|
10
|
|
|
|
|
4409
|
|
86
|
9
|
100
|
100
|
|
|
140
|
if (( ( $self->{_self} ) |
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
87
|
|
|
|
|
|
|
&& ( defined $self->{_config_file} ) |
88
|
|
|
|
|
|
|
&& ( ( $self->{_config_file} =~ '\(eval ' ) |
89
|
|
|
|
|
|
|
|| ( $self->{_config_file} =~ 'base.pm' ) ) |
90
|
|
|
|
|
|
|
) |
91
|
|
|
|
|
|
|
|| ( $_file eq $self->{_config_file} ) |
92
|
|
|
|
|
|
|
|| ( $0 eq $self->{_config_file} ) |
93
|
|
|
|
|
|
|
) |
94
|
|
|
|
|
|
|
{ |
95
|
2
|
|
|
|
|
14
|
return $self->_raise_error( |
96
|
|
|
|
|
|
|
q{Can't retrieve store from the calling file.}); |
97
|
|
|
|
|
|
|
} |
98
|
7
|
|
|
|
|
33
|
$file = $self->{_config_file}; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
12
|
100
|
|
|
|
179
|
return unless $self->_check_file($file); |
103
|
|
|
|
|
|
|
|
104
|
9
|
|
|
|
|
211
|
eval { $retrieved_hash_ref = lock_retrieve($file); }; |
|
9
|
|
|
|
|
31
|
|
105
|
|
|
|
|
|
|
|
106
|
9
|
100
|
|
|
|
1996
|
if ($@) { |
107
|
1
|
|
|
|
|
144
|
croak "ERROR: $@"; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
8
|
100
|
66
|
|
|
51
|
if ( ! ( $retrieved_hash_ref && ref $retrieved_hash_ref eq 'HASH') ) { |
111
|
1
|
|
|
|
|
7
|
return $self->_raise_error( |
112
|
|
|
|
|
|
|
q{Retrieved object isn't a HASH reference.}) |
113
|
|
|
|
|
|
|
}; |
114
|
|
|
|
|
|
|
|
115
|
7
|
|
|
|
|
13
|
$self->{_configuration} = $retrieved_hash_ref; |
116
|
|
|
|
|
|
|
|
117
|
7
|
100
|
|
|
|
36
|
return $self->{_configuration}->{$key} if $key; |
118
|
2
|
|
|
|
|
8
|
return $self->{_configuration}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# |
122
|
|
|
|
|
|
|
# SET STORABLE FILE |
123
|
|
|
|
|
|
|
# |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub set_storable_file { |
126
|
6
|
|
|
6
|
1
|
4458
|
my $self = shift; |
127
|
6
|
|
|
|
|
12
|
my $configuration_file = shift; |
128
|
|
|
|
|
|
|
|
129
|
6
|
100
|
|
|
|
25
|
if ( $self->_check_file( $configuration_file ) ) { |
130
|
5
|
|
|
|
|
142
|
$self->{_storable_file} = $configuration_file; |
131
|
5
|
|
|
|
|
9
|
$self->{_self} = 0; |
132
|
5
|
100
|
66
|
|
|
54
|
if ( ( $self->{_config_file} =~ '\(eval ' ) |
133
|
|
|
|
|
|
|
|| ($self->{_config_file} =~ 'base.pm' ) ) { |
134
|
3
|
|
|
|
|
9
|
delete $self->{_config_file}; |
135
|
|
|
|
|
|
|
} |
136
|
5
|
|
|
|
|
19
|
return $self; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
else { |
139
|
1
|
|
|
|
|
224
|
return; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
1; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
__END__ |