Hierarchical/Tie/ReadOnly.pm

filter: Statementstmt 100% 55 / 55 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 100% 74 / 74 CC 2 SCAR 1.1
CC
2
Cov
100%
TIEHASH
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Hierarchical/Delta.pm ↑ Summary
1  
2 package Config::Hierarchical::Tie::ReadOnly ;
3  
4 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 BEGIN
8 22 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #~ use Exporter ();
10  
11 22 ▶use vars qw ($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 22 $VERSION = '0.01' ;
14 22 @EXPORT_OK = qw ();
15 22 %EXPORT_TAGS = ();
16 }
17  
18 #-------------------------------------------------------------------------------
19  
20 22 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 22 ▶use base qw(Tie::Hash) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 22 ▶use English qw( -no_match_vars ) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 22 ▶use Readonly ;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 22 Readonly my $EMPTY_STRING => q{} ;
27  
28 =head1 NAME
29  
30 Config::Hierarchical::Tie::ReadOnly - Access Hierarchical configuration container through a read only hash
31  
32 =head1 SYNOPSIS
33  
34
35 my $config = new Config::Hierarchical
36 (
37 NAME => 'config',
38
39 CATEGORY_NAMES => ['A', 'B'],
40 DEFAULT_CATEGORY => 'B',
41
42 INITIAL_VALUES =>
43 [
44 {CATEGORY => 'A', NAME => 'CC1', VALUE => '1'},
45 {CATEGORY => 'B', NAME => 'CC2', VALUE => '2'},
46 {CATEGORY => 'A', NAME => 'CC3', VALUE => '3'},
47 {CATEGORY => 'B', NAME => 'CC4', VALUE => '4'},
48 {CATEGORY => 'A', NAME => 'CC5', VALUE => '5'},
49 ] ,
50 ) ;
51
52 my %hash ;
53 tie %hash, 'Config::Hierarchical::Tie::ReadOnly' => $config ;
54
55 my @keys = sort keys %hash ; # qw( CC1 CC2 CC3 CC4 CC5)
56 print $hash{CC1} ; # print '1'
57
58 $hash{CC1} = 2 ; # dies, hash is read only
59  
60 =head1 DESCRIPTION
61  
62 Creates a read only wrapper around a B<Config::Hierarchical> object. This let's you access the config
63 object as a hash. You can use B<{}> access which makes it easy to use the config in interpolated string.
64 You can also use B<keys> and B<each> on the tied config.
65  
66 but you can't modify the variables, clear the config or delete any variable.
67  
68 This is also class is also used to allow you to link a category to an existing Config::Hierarchical object. See
69 L<new> in <Config::Hierarchical>.
70  
71 =head1 DOCUMENTATION
72  
73 =head1 SUBROUTINES/METHODS
74  
75 =cut
76  
77 #-------------------------------------------------------------------------------
78  
79 sub TIEHASH
80 15 ▶{
Subroutine TIEHASH: called CC=2 SCAR=6.9
81 15 my ($class, @arguments) = @_ ;
82  
83 =head2 TIEHASH
84  
85 The method invoked by the command tie %hash, class name. Associates a new hash instance with the specified class.
86  
87 =cut
88  
89 15 ▶unless('Config::Hierarchical' eq ref $arguments[0])
Branch: 1 branch
BranchTrueFalseTotal
unless ("Config::Hierarchical" eq ref $arguments[0]) 1 14 15
90 {
91 1 croak "Argument must be a 'Config::Hierarchical' object!\n" ;
92 }
93
94 14 my $self = {CONFIG => $arguments[0]} ;
95 14 bless($self, $class) ;
96  
97 14 return($self) ;
98 }
99  
100 #-------------------------------------------------------------------------------
101  
102 sub STORE
103 2 ▶{ ## no critic (RequireFinalReturn)
Subroutine STORE: called CC=1 SCAR=0.0
104
105 2 my ($this, $key, $value) = @_ ;
106  
107 =head2 STORE
108  
109 Dies as this tie is read only.
110  
111 =cut
112  
113 2 my (undef, $filename, $line) = caller() ;
114 2 $this->{CONFIG}{INTERACTION}{DIE}->("This hash is read only at '$filename:$line'!\n") ;
115 }
116  
117 #-------------------------------------------------------------------------------
118  
119 sub FETCH
120 24 ▶{
Subroutine FETCH: called CC=1 SCAR=0.0
121  
122 24 my ($this, $key) = @_ ;
123  
124 =head2 FETCH
125  
126 Retrieve the value associated with the configuration variable passed as argument
127  
128 =cut
129  
130 24 my (undef, $filename, $line) = caller() ;
131 24 return($this->{CONFIG}->Get(NAME => $key, FILE => $filename, LINE => $line)) ;
132 }
133  
134 #-------------------------------------------------------------------------------
135  
136 sub FIRSTKEY
137 10 ▶{
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
138  
139 10 my ($this) = @_ ;
140  
141 =head2 FIRSTKEY
142  
143 Return the first key in the hash. Used internally by Perl.
144  
145 =cut
146  
147 10 $this->{KEYS} = [$this->{CONFIG}->GetKeys()] ;
148 10 $this->{KEY_INDEX} = 0 ;
149  
150 10 return $this->{KEYS}[$this->{KEY_INDEX}] ;
151 }
152  
153 #-------------------------------------------------------------------------------
154  
155 sub NEXTKEY
156 30 ▶{
Subroutine NEXTKEY: called CC=1 SCAR=0.0
157  
158 30 my ($this, $lastkey) = @_ ;
159  
160 =head2 NEXTKEY
161  
162 Return the next key in the hash. Used internally by Perl.
163  
164 =cut
165  
166 30 $this->{KEY_INDEX}++ ;
167 30 return $this->{KEYS}[$this->{KEY_INDEX}] ;
168 }
169  
170 #-------------------------------------------------------------------------------
171  
172 sub EXISTS
173 28 ▶{
Subroutine EXISTS: called CC=1 SCAR=0.0
174  
175 28 my ($this, $key) = @_ ;
176  
177 =head2 EXISTS
178  
179 Verify that key exists within the tied Config::Hierarchical.
180  
181 =cut
182  
183 28 return($this->{CONFIG}->Exists(NAME => $key)) ;
184 }
185  
186 #-------------------------------------------------------------------------------
187  
188 sub DELETE
189 1 ▶{ ## no critic (RequireFinalReturn)
Subroutine DELETE: called CC=1 SCAR=0.0
190  
191 1 my ($this, $key) = @_ ;
192  
193 =head2 DELETE
194  
195 Dies as this tie is read only.
196  
197 =cut
198  
199 1 my (undef, $filename, $line) = caller() ;
200 1 $this->{CONFIG}{INTERACTION}{DIE}->("This hash is read only at '$filename:$line'!\n") ;
201 }
202  
203 #-------------------------------------------------------------------------------
204  
205 sub CLEAR
206 1 ▶{ ## no critic (RequireFinalReturn)
Subroutine CLEAR: called CC=1 SCAR=0.0
207  
208 1 my ($this) = @_ ;
209  
210 =head2 CLEAR
211  
212 Dies as this tie is read only.
213  
214 =cut
215  
216 1 my (undef, $filename, $line) = caller() ;
217 1 $this->{CONFIG}{INTERACTION}{DIE}->("This hash is read only at '$filename:$line'!\n") ;
218 }
219  
220 #-------------------------------------------------------------------------------
221  
222 sub SCALAR
223 1 ▶{
Subroutine SCALAR: called CC=1 SCAR=0.0
224  
225 1 my ($this) = @_ ;
226  
227 =head2 SCALAR
228  
229 returns the number of elements in the tied Config::Hierarchical object.
230  
231 =cut
232  
233 1 return($this->{CONFIG}->GetKeys()) ;
234 }
235
236 #-------------------------------------------------------------------------------
237  
238 22 1 ;
239  
240 =head1 BUGS AND LIMITATIONS
241  
242 None so far.
243  
244 =head1 AUTHOR
245  
246 Khemir Nadim ibn Hamouda
247 CPAN ID: NKH
248 mailto:nadim@khemir.net
249  
250 =head1 LICENSE AND COPYRIGHT
251  
252 Copyright 2007 Khemir Nadim. All rights reserved.
253  
254 This program is free software; you can redistribute
255 it and/or modify it under the same terms as Perl itself.
256  
257 =head1 SUPPORT
258  
259 You can find documentation for this module with the perldoc command.
260  
261 perldoc Config::Hierarchical
262  
263 You can also look for information at:
264  
265 =over 4
266  
267 =item * AnnoCPAN: Annotated CPAN documentation
268  
269 L<http://annocpan.org/dist/Config-Hierarchical>
270  
271 =item * RT: CPAN's request tracker
272  
273 Please report any bugs or feature requests to L <bug-config-hierarchical@rt.cpan.org>.
274  
275 We will be notified, and then you'll automatically be notified of progress on
276 your bug as we make changes.
277  
278 =item * Search CPAN
279  
280 L<http://search.cpan.org/dist/Config-Hierarchical>
281  
282 =back
283  
284 =head1 SEE ALSO
285  
286  
287 =cut
« Hierarchical/Delta.pm ↑ Summary