blib/lib/ChordPro/Config/Properties.pm

filter: Statementstmt 76.5% 255 / 333 Branchbran 69.1% 119 / 172 Conditioncond 51.6% 31 / 60 MC/DCmcdc 27.7% 10 / 36 Subroutinesub 64.1% 25 / 39 Podpod n/a totaltotal 68.7% 440 / 640 CC 117 SCAR 30.2
CC
117
Cov
72%
_parse_file_internal
56.0
_parse_lines_internal
44.5
_constructor
24.3
CRAP 20.4
« lib/ChordPro/lib/SVGPDF/Use.pm ↑ Summary blib/lib/ChordPro/Wx/Config.pm »
1 #! perl
2  
3 package Data::Properties;
4  
5 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Author : Johan Vromans
9 # Created On : Mon Mar 4 11:51:54 2002
10 # Last Modified By:
11 # Last Modified On: Wed Jan 22 14:14:14 2025
12 # Update Count : 559
13 # Status : Unknown, Use with caution!
14  
15 =head1 NAME
16  
17 Data::Properties -- Flexible properties handling
18  
19 =head1 SUMMARY
20  
21 use Data::Properties;
22  
23 my $cfg = new Data::Properties;
24  
25 # Preset a property.
26 $cfg->set_property("config.version", "1.23");
27  
28 # Parse a properties file.
29 $cfg->parse_file("config.prp");
30  
31 # Get a property value
32 $version = $cfg->get_property("config.version");
33 # Same, but with a default value.
34 $version = $cfg->get_property("config.version", "1.23");
35  
36 # Get the list of subkeys for a property, and process them.
37 my $aref = $cfg->get_property_keys("item.list");
38 foreach my $item ( @$aref ) {
39 if ( $cfg->get_property("item.list.$item") ) {
40 ....
41 }
42 }
43  
44 =head1 DESCRIPTION
45  
46 The property mechanism is modelled after the Java implementation of
47 properties.
48  
49 In general, a property is a string value that is associated with a
50 key. A key is a series of names (identifiers) separated with periods.
51 Names are treated case insensitive. Unlike in Java, the properties are
52 really hierarchically organized. This means that for a given property
53 you can fetch the list of its subkeys, and so on. Moreover, the list
54 of subkeys is returned in the order the properties were defined.
55  
56 Data::Properties can also be used to define data structures, just like
57 JSON but with much less quotes.
58  
59 Property lookup can use a preset property context. If a context I<ctx>
60 has been set using C<set_context('I<ctx>')>,
61 C<get_property('foo.bar')> will first try C<'I<ctx>.foo.bar'> and
62 then C<'foo.bar'>. C<get_property('.foo.bar')> (note the leading
63 period) will only try C<'I<ctx>.foo.bar'> and raise an exception if
64 no context was set.
65  
66 Design goals:
67  
68 =over
69  
70 =item *
71  
72 properties must be hierarchical of unlimited depth;
73  
74 =item *
75  
76 manual editing of the property files (hence unambiguous syntax and lay out);
77  
78 =item *
79  
80 it must be possible to locate all subkeys of a property in the
81 order they appear in the property file(s);
82  
83 =item *
84  
85 lightweight so shell scripts can use it to query properties.
86  
87 =back
88  
89 =cut
90  
91 13 our $VERSION = "2.001";
92  
93 13 ▶use Text::ParseWords qw(parse_line);
Subroutine BEGIN: called CC=1 SCAR=0.0
94 13 ▶use ChordPro::Files;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 13 ▶use String::Interpolate::Named;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 13 my $DEBUG = 1;
99  
100 ################ Constructors ################
101  
102 =over
103  
104 =item new
105  
106 I<new> is the standard constructor. I<new> doesn't require any
107 arguments, but you can pass it a list of initial properties to store
108 in the resultant properties object.
109  
110 =cut
111  
112 24 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
113 24 ▶ if ( ref($_[1]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1]) 0 24 24
114 # IX/Data-Properties.
115 0 croak("API Error -- Incompatible Data::Properties version");
116 }
117 24 unshift(@_, 0);
118 24 &_constructor;
119 }
120  
121 =item clone
122  
123 I<clone> is like I<new>, but it takes an existing properties object as
124 its invocant and returns a new object with the contents copied.
125  
126 B<WARNING> This is not a deep copy, so take care.
127  
128 =cut
129  
130 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
131 0 unshift(@_, 1);
132 0 &_constructor;
133 }
134  
135 # Internal construction helper.
136 24 ▶sub _constructor {
Subroutine _constructor: called CC=8 SCAR=31.9
137 # Get caller and initial attributes.
138 24 my ($cloning, $invocant, %atts) = @_;
139  
140 # If the invocant is an object, get its class.
141 24 ▶ my $class = ref($invocant) || $invocant;
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
0240
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
142  
143 # Initialize and bless the new object.
144 24 my $self = bless({}, $class);
145  
146 # Default path.
147 24 $self->{_path} = [ "." ];
148  
149 # Initialize.
150 0 ▶ $self->{_props} = $cloning ? {%{$invocant->{_props}}} : {};
Branch: 1 branch
BranchTrueFalseTotal
$cloning ? : 0 24 24
151  
152 # Fill in initial attribute values.
153 24 ▶ while ( my ($k, $v) = each(%atts) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %atts) 0 24 24
154 0 ▶ if ( $k eq "_context" ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($k eq '_context') { } 0 0 0
elsif ($k eq '_debug') { } 0 0 0
elsif ($k eq '_noinc') { } 0 0 0
elsif ($k eq '_raw') { } 0 0 0
155 0 $self->{_context} = $v;
156 }
157 elsif ( $k eq "_debug" ) {
158 0 $self->{_debug} = 1;
159 }
160 elsif ( $k eq "_noinc" ) {
161 0 $self->{_noinc} = 1;
162 }
163 elsif ( $k eq "_raw" ) {
164 0 $self->{_raw} = 1;
165 }
166 else {
167 0 $self->set_property($k, $v);
168 }
169 }
170 24 $self->{_in_context} = undef;
171  
172 # Return.
173 24 $self;
174 }
175  
176 ################ Methods ################
177  
178 =item parse_file I<file> [ , I<context> ]
179  
180 I<parse_file> reads a properties file and adds the contents to the
181 properties object.
182  
183 I<file> is the name of the properties file. This file is searched in
184 all elements of the current search path (see L<set_path()|/"set_path I<paths>">) unless
185 the name starts with a slash.
186  
187 I<context> can be used to designate an initial context where all
188 properties from the file will be subkeys of.
189  
190 For the detailed format of properties files see L<PROPERTY FILES>.
191  
192 Reading the file is handled by L<File::LoadLines>. See its
193 documentation for more power.
194  
195 =cut
196  
197 0 ▶sub parse_file {
Subroutine parse_file: not called CC=2 SCAR=12.6
198 0 my ($self, $file, $context) = @_;
199 0 $self->_parse_file_internal( $file, $context);
200  
201 0 ▶ if ( $self->{_debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
202 13 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
203 0 $Data::Dumper::Indent = 2;
204 0 warn(Data::Dumper->Dump([$self->{_props}],[qw(properties)]), "\n");
205 }
206 0 $self;
207 }
208  
209 =item parse_lines I<lines> [ , I<filename> [ , I<context> ] ]
210  
211 As I<parse_file>, but processes an array of lines.
212  
213 I<filename> is used for diagnostic purposes only.
214  
215 I<context> can be used to designate an initial context where all
216 properties from the file will be subkeys of.
217  
218 =cut
219  
220 27 ▶sub parse_lines {
Subroutine parse_lines: called CC=2 SCAR=7.3
221 27 my ($self, $lines, $file, $context) = @_;
222 27 $self->_parse_lines_internal( $lines, $file, $context);
223  
224 27 ▶ if ( $self->{_debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 27 27
225 13 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
226 0 $Data::Dumper::Indent = 2;
227 0 warn(Data::Dumper->Dump([$self->{_props}],[qw(properties)]), "\n");
228 }
229 27 $self;
230 }
231  
232 # Catch some calls that are not in this version of Data::Properties.
233 0 ▶sub load {
Subroutine load: not called CC=1 SCAR=6.9
234 0 croak("API Error -- Incompatible Data::Properties version");
235 }
236 0 ▶sub property_names {
Subroutine property_names: not called CC=1 SCAR=6.9
237 0 croak("API Error -- Incompatible Data::Properties version");
238 }
239 0 ▶sub store {
Subroutine store: not called CC=1 SCAR=6.9
240 0 croak("API Error -- Incompatible Data::Properties version");
241 }
242  
243 =item set_path I<paths>
244  
245 Sets a search path for file lookup.
246  
247 I<paths> must be reference to an array of paths.
248  
249 Default I<path> is C<[ '.' ]> (current directory).
250  
251 =item get_path
252  
253 Gets the current search path for file lookup.
254  
255 =cut
256  
257 0 ▶sub set_path {
Subroutine set_path: not called CC=3 SCAR=24.8
258 0 my ( $self ) = shift;
259 0 my $path = shift;
260 0 ▶ if ( @_ > 0 || !UNIVERSAL::isa($path,'ARRAY') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0 or not UNIVERSAL::isa($path, "ARRAY")) 0 0 0
Condition0%0/3
@_ > 0 or not UNIVERSAL::isa($path, "ARRAY")
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ > 0 or not UNIVERSAL::isa($path, "ARRAY")
@_ > 0 not UNIVERSAL::isa($path, "ARRAY")
Truth table0%0/3
A or not(B)
A= @_ > 0B= not UNIVERSAL::isa($path, "ARRAY")
ABresult
1X1
011
000
261 0 $path = [ $path, @_ ];
262 }
263 0 $self->{_path} = $path;
264 }
265  
266 0 ▶sub get_path {
Subroutine get_path: not called CC=1 SCAR=6.9
267 0 my ( $self ) = @_;
268 0 $self->{_path};
269 }
270  
271 # internal
272  
273 0 ▶sub _parse_file_internal {
Subroutine _parse_file_internal: not called CC=7 SCAR=40.3
274  
275 0 my ($self, $file, $context) = @_;
276 0 my $did = 0;
277 0 my $searchpath = $self->{_path};
278 0 ▶ $searchpath = [ '' ] unless $searchpath;
Branch: 1 branch
BranchTrueFalseTotal
unless $searchpath 0 0 0
279  
280 0 foreach ( @$searchpath ) {
281 0 my $path = $_;
282 0 ▶ $path .= "/" unless $path eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $path eq "" 0 0 0
283  
284 # Fetch one.
285 0 my $cfg = $file;
286 0 ▶ $cfg = $path . $file unless $file =~ m:^/:;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ m[^/] 0 0 0
287 0 ▶ next unless fs_test( e => $cfg );
Branch: 1 branch
BranchTrueFalseTotal
unless fs_test("e", $cfg) 0 0 0
288  
289 0 my $opt = { strip => qr/[ \t]*\\(?:\r\n|\n|\r)[ \t]*/ };
290 0 my $lines = fs_load( $cfg, $opt );
291 0 $self->parse_lines( $lines, $cfg, $context );
292 0 $did++;
293  
294 # We read a file, no need to proceed searching.
295 0 last;
296 }
297  
298 # Sanity checks.
299 0 ▶ croak("No properties $file in " . join(":", @$searchpath)) unless $did;
Branch: 1 branch
BranchTrueFalseTotal
unless $did 0 0 0
300 }
301  
302 # internal
303  
304 1040 ▶sub _value {
Subroutine _value: called CC=10 SCAR=23.1
305 1040 my ( $self, $value, $ctx, $noexpand ) = @_;
306  
307 # Single-quoted string.
308 1040 ▶ if ( $value =~ /^'(.*)'\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^'(.*)'\s*$/) 221 819 1040
309 221 $value = $1;
310 221 $value =~ s/\\\\/\x{fdd0}/g;
311 221 $value =~ s/\\'/'/g;
312 221 $value =~ s/\x{fdd0}/\\/g;
313 221 return $value;
314 }
315  
316 819 ▶ if ( $self->{_raw} && $value =~ /^(null|false|true)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_raw"} and $value =~ /^(null|false|true)$/) 0 819 819
Condition33%1/3
$self->{"_raw"} and $value =~ /^(null|false|true)$/
!ll&&!rl&&r
81900
MC/DC0%0/2
$self->{"_raw"} and $value =~ /^(null|false|true)$/
$self->{"_raw"} $value =~ /^(null|false|true)$/
Truth table33%1/3
A and B
A= $self->{"_raw"}B= $value =~ /^(null|false|true)$/
ABresult
0X0
100
111
317 0 return $value;
318 }
319  
320 819 ▶ if ( lc($value) eq "null" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $value eq "null") 15 804 819
321 15 return;
322 }
323 804 ▶ if ( lc($value) eq "true" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $value eq "true") 4 800 804
324 4 return 1;
325 }
326 800 ▶ if ( lc($value) eq "false" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $value eq "false") 23 777 800
327 23 return 0;
328 }
329  
330 777 ▶ if ( $value =~ /^"(.*)"\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^"(.*)"\s*$/) 167 610 777
331 167 $value = $1;
332 167 $value =~ s/\\\\/\x{fdd0}/g;
333 167 $value =~ s/\\"/"/g;
334 167 $value =~ s/\\n/\n/g;
335 167 $value =~ s/\\t/\t/g;
336 167 $value =~ s/\\([0-7]{1,3})/sprintf("%c",oct($1))/ge;
337 167 $value =~ s/\\x([0-9a-f][0-9a-f]?)/sprintf("%c",hex($1))/ge;
338 167 $value =~ s/\\x\{([0-9a-f]+)\}/sprintf("%c",hex($1))/ge;
339 167 $value =~ s/\x{fdd0}/\\/g;
340 167 ▶ return $value if $noexpand;
Branch: 1 branch
BranchTrueFalseTotal
if $noexpand 2 165 167
341 165 return $self->expand($value, $ctx);
342 }
343  
344 610 ▶ return $value if $noexpand;
Branch: 1 branch
BranchTrueFalseTotal
if $noexpand 502 108 610
345 108 $self->expand($value, $ctx);
346 }
347  
348 27 ▶sub _parse_lines_internal {
Subroutine _parse_lines_internal: called CC=40 SCAR=38.0
349  
350 27 my ( $self, $lines, $filename, $context ) = @_;
351  
352 27 ▶ my @stack = $context ? ( [$context, undef] ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$context ? : 9 18 27
353 27 my $keypat = qr/[-\w.]+|"[^"]*"|'[^']*'/;
354  
355 # Process its contents.
356 27 my $lineno = 0;
357 27 ▶ while ( @$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 873 27 900
358 873 $lineno++;
359 873 $_ = shift(@$lines);
360  
361 #### Discard empty lines and comment lines/
362 873 ▶ next if /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ 226 647 873
363 647 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 36 611 647
364  
365 #### Trim.
366 611 s/^\s+//;
367 611 s/\s+$//;
368  
369 #### Controls
370 # include filename (only if at the line start, and not followed by =.
371 611 ▶ if ( /^include\s+((?![=:]).+)/ && !$self->{_noinc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^include\s+((?![=:]).+)/ and not $self->{"_noinc"}) 0 611 611
Condition33%1/3
/^include\s+((?![=:]).+)/ and not $self->{"_noinc"}
!ll&&!rl&&r
61100
MC/DC0%0/2
/^include\s+((?![=:]).+)/ and not $self->{"_noinc"}
/^include\s+((?![=:]).+)/ not $self->{"_noinc"}
Truth table33%1/3
A and not(B)
A= /^include\s+((?![=:]).+)/B= not $self->{"_noinc"}
ABresult
0X0
100
111
372 0 my $value = $self->_value( $1, $stack[0] );
373 0 $self->_parse_file_internal($value, $stack[0]);
374 0 next;
375 }
376  
377 #### Settings
378 # key = value
379 # key {
380 # key [
381 # value
382 # ]
383 # }
384  
385 # foo.bar {
386 # foo.bar [
387 # Push a new context.
388 611 ▶ if ( /^($keypat)\s*([{])$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^($keypat)\s*([{])$/) 48 563 611
389 48 my $c = $self->_value( $1, undef, "noexpand" );
390 48 ▶ my $i = $2 eq '[' ? 0 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq '[' ? : 0 48 48
391 48 ▶ @stack = ( [ $c, $i ] ), next unless @stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @stack 13 35 48
392 35 unshift( @stack, [ $stack[0]->[0] . "." . $c, $i ] );
393 35 next;
394 }
395 563 ▶ if ( /^($keypat)\s*[:=]\s*([[])$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^($keypat)\s*[:=]\s*([[])$/) 9 554 563
396 9 my $c = $self->_value( $1, undef, "noexpand" );
397 9 ▶ my $i = $2 eq '[' ? 0 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq '[' ? : 9 0 9
398 9 ▶ @stack = ( [ $c, $i ] ), next unless @stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @stack 1 8 9
399 8 unshift( @stack, [ $stack[0]->[0] . "." . $c, $i ] );
400 8 next;
401 }
402  
403 # foo.bar = [ val val ]
404 # foo.bar = [ val
405 # val ]
406 # foo.bar = [ val val
407 # ]
408 # BUT NOT
409 # foo.bar = [
410 # val val ]
411 # Create an array
412 # Add lines, if necessary.
413 554 ▶ while ( /^($keypat)\s*[=:]\s*\[(.+)$/ && $2 !~ /\]\s*$/ && @$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^($keypat)\s*[=:]\s*\[(.+)$/ and not $2 =~ /\]\s*$/ and @$lines) 2 554 556
Condition83%5/6
/^($keypat)\s*[=:]\s*\[(.+)$/ and not $2 =~ /\]\s*$/ and @$lines
!ll&&!rl&&r
55402
/^($keypat)\s*[=:]\s*\[(.+)$/ and not $2 =~ /\]\s*$/
!ll&&!rl&&r
542122
MC/DC66%2/3
/^($keypat)\s*[=:]\s*\[(.+)$/ and not $2 =~ /\]\s*$/ and @$lines
/^($keypat)\s*[=:]\s*\[(.+)$/ not $2 =~ /\]\s*$/ @$lines
Truth table75%3/4
A and not(B) and C
A= /^($keypat)\s*[=:]\s*\[(.+)$/B= not $2 =~ /\]\s*$/C= @$lines
ABCresult
0XX0
10X0
1100
1111
414 2 $_ .= " " . shift(@$lines);
415 2 $lineno++;
416 }
417 554 ▶ if ( /^($keypat)\s*[:=]\s*\[(.*)\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^($keypat)\s*[:=]\s*\[(.*)\]$/) 12 542 554
418 12 my $prop = $self->_value( $1, undef, "noexpand" );
419 12 ▶ $prop = $stack[0]->[0] . "." . $prop if @stack;
Branch: 1 branch
BranchTrueFalseTotal
if @stack 9 3 12
420 12 my $v = $2;
421 12 $v =~ s/^\s+//;
422 12 $v =~ s/\s+$//;
423 12 my $ix = 0;
424 12 for my $value ( parse_line( '\s+', 1, $v ) ) {
425 24 $value = $self->_value( $value, $stack[0] );
426 24 $self->set_property( $prop . "." . $ix++, $value );
427 }
428 12 ▶ $self->set_property( $prop, undef ) unless $ix;
Branch: 1 branch
BranchTrueFalseTotal
unless $ix 3 9 12
429 12 next;
430 }
431  
432 542 ▶ if ( /^\s*\[(.*)\]$/ && @stack && $stack[0][1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\[(.*)\]$/ and @stack and $stack[0][1]) 10 532 542
Condition66%4/6
/^\s*\[(.*)\]$/ and @stack and $stack[0][1]
!ll&&!rl&&r
532010
/^\s*\[(.*)\]$/ and @stack
!ll&&!rl&&r
532010
MC/DC33%1/3
/^\s*\[(.*)\]$/ and @stack and $stack[0][1]
/^\s*\[(.*)\]$/ @stack $stack[0][1]
Truth table50%2/4
A and B and C
A= /^\s*\[(.*)\]$/B= @stackC= $stack[0][1]
ABCresult
0XX0
10X0
1100
1111
433 10 my $prop = $stack[0][0] . "." . $stack[0][1]++;
434 10 my $v = $1;
435 10 $v =~ s/^\s+//;
436 10 $v =~ s/\s+$//;
437 10 my $ix = 0;
438 10 for my $value ( parse_line( '\s+', 1, $v ) ) {
439 32 $value = $self->_value( $value, $stack[0] );
440 32 $self->set_property( $prop . "." . $ix++, $value );
441 }
442 10 next;
443 }
444  
445 # {
446 # [
447 # Push a new context while building an array.
448 532 ▶ if ( @stack && defined($stack[0]->[1]) # building array
Branch: 1 branch
BranchTrueFalseTotal
if (@stack and defined $stack[0][1] and /^([{\[])$/) 7 525 532
Condition100%6/6
@stack and defined $stack[0][1] and /^([{\[])$/
!ll&&!rl&&r
493327
@stack and defined $stack[0][1]
!ll&&!rl&&r
26722639
MC/DC100%3/3
@stack and defined $stack[0][1] and /^([{\[])$/
@stack defined $stack[0][1] /^([{\[])$/
Truth table100%4/4
A and B and C
A= @stackB= defined $stack[0][1]C= /^([{\[])$/
ABCresult
0XX0
10X0
1100
1111
449 && /^([{\[])$/ ) {
450 7 ▶ my $i = $1 eq '[' ? 0 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '[' ? : 0 7 7
451 7 unshift( @stack, [ $stack[0]->[0] . "." . $stack[0]->[1]++, $i ] );
452 7 next;
453 }
454  
455 # }
456 # ]
457 # Pop context.
458 525 ▶ if ( /^([}\]])$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^([}\]])$/) 64 461 525
459 64 ▶ die("stack underflow at line $lineno")
Branch: 2 branches
BranchTrueFalseTotal
unless @stack and $1 eq defined $stack[0][1] ? "]" : "}" 0 64 64
$1 eq defined $stack[0][1] ? : 0 64 64
Condition33%1/3
@stack and $1 eq defined $stack[0][1] ? "]" : "}"
!ll&&!rl&&r
0064
MC/DC0%0/2
@stack and $1 eq defined $stack[0][1] ? "]" : "}"
@stack $1 eq defined $stack[0][1] ? "]" : "}"
Truth table33%1/3
A and B
A= @stackB= $1 eq defined $stack[0][1] ? "]" : "}"
ABresult
0X0
100
111
460 unless @stack
461 && ( $1 eq defined($stack[0]->[1]) ? ']' : '}' );
462 64 shift(@stack);
463 64 next;
464 }
465  
466 # foo.bar = blech
467 # foo.bar = "blech"
468 # foo.bar = 'blech'
469 # Simple assignment.
470 # The value is expanded unless single quotes are used.
471 461 ▶ if ( /^($keypat)\s*[=:]\s*(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^($keypat)\s*[=:]\s*(.*)/) 438 23 461
472 438 ▶ die("Brace is illegal as a value (use quotes to bypass)\n")
Branch: 1 branch
BranchTrueFalseTotal
if $2 eq "{" 0 438 438
473 if $2 eq '{';
474 438 my $prop = $self->_value( $1, undef, "noexpand" );
475 438 my $value = $self->_value( $2, $stack[0] );
476  
477 # Make a full name.
478 438 ▶ $prop = $stack[0]->[0] . "." . $prop if @stack;
Branch: 1 branch
BranchTrueFalseTotal
if @stack 171 267 438
479  
480 # Set the property.
481 438 $self->set_property($prop, $value);
482  
483 438 next;
484 }
485  
486 # value(s) (while building an array)
487 23 ▶ if ( @stack && defined($stack[0]->[1]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack and defined $stack[0][1]) 23 0 23
Condition33%1/3
@stack and defined $stack[0][1]
!ll&&!rl&&r
0023
MC/DC0%0/2
@stack and defined $stack[0][1]
@stack defined $stack[0][1]
Truth table33%1/3
A and B
A= @stackB= defined $stack[0][1]
ABresult
0X0
100
111
488  
489 23 for my $value ( parse_line( '\s+', 1, $_ ) ) {
490 # Make a full name.
491 39 my $prop = $stack[0]->[0] . "." . $stack[0]->[1]++;
492  
493 39 $value = $self->_value( $value, $stack[0] );
494  
495 # Set the property.
496 39 $self->set_property($prop, $value);
497 }
498 23 next;
499 }
500  
501 # Error.
502 0 croak("?line $lineno: $_\n");
503 }
504  
505 # Sanity checks.
506 27 ▶ croak("Unfinished properties $filename")
Branch: 2 branches
BranchTrueFalseTotal
if @stack != ($context ? 1 : 0) 0 27 27
$context ? : 9 18 27
507 if @stack != ($context ? 1 : 0);
508 }
509  
510 =item get_property I<prop> [ , I<default> ]
511  
512 Get the value for a given property I<prop>.
513  
514 If a context I<ctx> has been set using C<set_context('I<ctx>')>,
515 C<get_property('foo.bar')> will first try C<'I<ctx>.foo.bar'> and then
516 C<'foo.bar'>. C<get_property('.foo.bar')> (note the leading period)
517 will only try C<'I<ctx>.foo.bar'> and raise an exception if no context
518 was set.
519  
520 If no value can be found, I<default> is used.
521  
522 In either case, the resultant value is examined for references to
523 other properties or environment variables. See L<PROPERTY FILES> below.
524  
525 =cut
526  
527 4 ▶sub get_property {
Subroutine get_property: called CC=1 SCAR=0.0
528 4 my ($self) = shift;
529 4 $self->expand($self->get_property_noexpand(@_));
530 }
531  
532 =item get_property_noexpand I<prop> [ , I<default> ]
533  
534 This is like I<get_property>, but does not do any expansion.
535  
536 =cut
537  
538 4 ▶sub get_property_noexpand {
Subroutine get_property_noexpand: called CC=9 SCAR=31.7
539 4 my ($self, $prop, $default) = @_;
540 4 $prop = lc($prop);
541 4 my $ctx = $self->{_context};
542 4 my $context_only;
543 4 ▶ if ( ($context_only = $prop =~ s/^\.//) && !$ctx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context_only = $prop =~ s/^\.// and not $ctx) 0 4 4
Condition33%1/3
$context_only = $prop =~ s/^\.// and not $ctx
!ll&&!rl&&r
400
MC/DC0%0/2
$context_only = $prop =~ s/^\.// and not $ctx
$context_only = $prop =~ s/^\.// not $ctx
Truth table33%1/3
A and not(B)
A= $context_only = $prop =~ s/^\.//B= not $ctx
ABresult
0X0
100
111
544 0 croak("get_property: no context for $prop");
545 }
546 4 ▶ if ( defined($ctx) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctx) 0 4 4
547 0 ▶ $ctx .= "." if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 0 0 0
548 0 ▶ if ( exists($self->{_props}->{$ctx.$prop}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_props"}{$ctx . $prop}) 0 0 0
549 0 $self->{_in_context} = $ctx;
550 0 return $self->{_props}->{$ctx.$prop};
551 }
552 }
553 4 ▶ if ( $context_only ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context_only) 0 4 4
554 0 $self->{_in_context} = undef;
555 0 return $default;
556 }
557 4 ▶ if ( defined($self->{_props}->{$prop}) && $self->{_props}->{$prop} ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_props"}{$prop} and $self->{"_props"}{$prop} ne "") 4 0 4
Condition33%1/3
defined $self->{"_props"}{$prop} and $self->{"_props"}{$prop} ne ""
!ll&&!rl&&r
004
MC/DC0%0/2
defined $self->{"_props"}{$prop} and $self->{"_props"}{$prop} ne ""
defined $self->{"_props"}{$prop} $self->{"_props"}{$prop} ne ""
Truth table33%1/3
A and B
A= defined $self->{"_props"}{$prop}B= $self->{"_props"}{$prop} ne ""
ABresult
0X0
100
111
558 4 $self->{_in_context} = "";
559 4 return $self->{_props}->{$prop};
560 }
561 0 $self->{_in_context} = undef;
562 0 $default;
563 }
564  
565 =item gps I<prop> [ , I<default> ]
566  
567 This is like I<get_property>, but raises an exception if no value
568 could be established.
569  
570 This is probably the best and safest method to use.
571  
572 =cut
573  
574 3 ▶sub gps {
Subroutine gps: called CC=3 SCAR=11.8
575 3 my $nargs = @_;
576 3 my ($self, $prop, $default) = @_;
577 3 my $ret = $self->get_property($prop, $default);
578 3 ▶ croak("gps: no value for $prop")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret or $nargs == 3 0 3 3
Condition33%1/3
defined $ret or $nargs == 3
l!l&&r!l&&!r
300
MC/DC0%0/2
defined $ret or $nargs == 3
defined $ret $nargs == 3
Truth table33%1/3
A or B
A= defined $retB= $nargs == 3
ABresult
1X1
011
000
579 unless defined($ret) || $nargs == 3;
580 3 $ret;
581 }
582  
583 =item get_property_keys I<prop>
584  
585 Returns an array reference with the names of the (sub)keys for the
586 given property. The names are unqualified, e.g., when properties
587 C<foo.bar> and C<foo.blech> exist, C<get_property_keys('foo')> would
588 return C<['bar', 'blech']>.
589  
590 =cut
591  
592 0 ▶sub get_property_keys {
Subroutine get_property_keys: not called CC=2 SCAR=17.9
593 0 my ($self, $prop) = @_;
594 0 ▶ $prop .= '.' if $prop;
Branch: 1 branch
BranchTrueFalseTotal
if $prop 0 0 0
595 0 $prop .= '@';
596 0 $self->get_property_noexpand($prop);
597 }
598  
599 =item expand I<value> [ , I<context> ]
600  
601 Perform the expansion as described with I<get_property>.
602  
603 =cut
604  
605 762 ▶sub expand {
Subroutine expand: called CC=4 SCAR=14.5
606 762 my ($self, $ret, $ctx) = (@_, "");
607 762 ▶ return $ret unless $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 91 671 762
608 671 ▶ warn("expand($ret,",$ctx//'<undef>',")\n") if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 671 671
Condition0%0/2
$ctx // "<undef>"
l!l
00
MC/DC0%0/1
$ctx // "<undef>"
$ctx
Truth table0%0/2
A
A= $ctx
Aresult
11
00
609 671 my $props = $self->{_props};
610 671 $ret =~ s:^~(/|$):$ENV{HOME}$1:g;
611 671 return $self->_interpolate( $ret, $ctx );
612 }
613  
614 # internal
615  
616 671 ▶sub _interpolate {
Subroutine _interpolate: called CC=2 SCAR=7.0
617 671 my ( $self, $tpl, $ctx ) = @_;
618 671 ▶ ( $ctx, my $ix ) = @$ctx if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 214 457 671
619 671 my $props = $self->{_props};
620 return interpolate( { activator => '$',
621 keypattern => qr/\.?\w+[-_\w.]*\??(?::.*)?/,
622 14 ▶ args => sub {
Subroutine __ANON__: called CC=10 SCAR=23.2
623 14 my $key = shift;
624 14 ▶ warn("_inter($key,",$ctx//'<undef>',")\n") if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 14 14
Condition0%0/2
$ctx // "<undef>"
l!l
00
MC/DC0%0/1
$ctx // "<undef>"
$ctx
Truth table0%0/2
A
A= $ctx
Aresult
11
00
625 # Establish the value for this key.
626 14 my $val = '';
627  
628 14 my $default = '';
629 14 ▶ ( $key, $default ) = ( $1, $2 )
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^(.*?):(.*)/ 3 11 14
630 if $key =~ /^(.*?):(.*)/;
631 14 my $checkdef = $key =~ s/\?$//;
632  
633 # If an environment variable exists, take its value.
634 14 ▶ if ( exists($ENV{$key}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{$key}) { } 1 13 14
635 1 $val = $ENV{$key};
636 1 ▶ $val = defined($val) if $checkdef;
Branch: 1 branch
BranchTrueFalseTotal
if $checkdef 0 1 1
637 }
638 else {
639 13 my $orig = $key;
640 13 ▶ $key = $ctx.$key if ord($key) == ord('.');
Branch: 1 branch
BranchTrueFalseTotal
if ord $key == 46 3 10 13
641 # For properties, the value should be non-empty.
642 13 ▶ if ( $checkdef ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($checkdef) { } 2 11 13
elsif (defined $$props{lc $key} and $$props{lc $key} ne '') { } 6 5 11
643 2 $val = defined($props->{lc($key)});
644 }
645 ▶ elsif ( defined($props->{lc($key)}) && $props->{lc($key)} ne "" ) {
Condition100%3/3
defined $$props{lc $key} and $$props{lc $key} ne ""
!ll&&!rl&&r
416
MC/DC100%2/2
defined $$props{lc $key} and $$props{lc $key} ne ""
defined $$props{lc $key} $$props{lc $key} ne ""
Truth table100%3/3
A and B
A= defined $$props{lc $key}B= $$props{lc $key} ne ""
ABresult
0X0
100
111
646 6 $val = $props->{lc($key)};
647 }
648 else {
649 5 $val = $default;
650 }
651 }
652 14 return $val;
653 } },
654 671 $tpl );
655 }
656  
657 =item set_property I<prop>, I<value>
658  
659 Set the property to the given value.
660  
661 =cut
662  
663 537 ▶sub set_property {
Subroutine set_property: called CC=4 SCAR=13.9
664 537 my ($self, $prop, $value) = @_;
665 537 my $props = $self->{_props};
666 537 $props->{lc($prop)} = $value;
667 537 my @prop = split(/\./, $prop, -1);
668 537 ▶ while ( @prop ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@prop) 1660 537 2197
669 1660 my $last = pop(@prop);
670 1660 my $p = lc(join(".", @prop, '@'));
671 1660 ▶ if ( exists($props->{$p}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $props->{$p}) { } 1466 194 1660
672 510 push(@{$props->{$p}}, $last)
673 1466 ▶ unless index(join("\0","",@{$props->{$p}},""),
Branch: 1 branch
BranchTrueFalseTotal
unless index(join("\0", "", @{$props->{$p};}, ""), "\000" . $last . "\000") != -1 510 956 1466
674 "\0".$last."\0") >= 0;
675 }
676 else {
677 194 $props->{$p} = [ $last ];
678 }
679 }
680 }
681  
682 =item set_properties I<prop1> => I<value1>, ...
683  
684 Add a hash (key/value pairs) of properties to the set of properties.
685  
686 =cut
687  
688 0 ▶sub set_properties {
Subroutine set_properties: not called CC=2 SCAR=17.9
689 0 my ($self, %props) = @_;
690 0 foreach ( keys(%props) ) {
691 0 $self->set_property($_, $props{$_});
692 }
693 }
694  
695 =item set_context I<context>
696  
697 Set the search context. Without argument, clears the current context.
698  
699 =cut
700  
701 0 ▶sub set_context {
Subroutine set_context: not called CC=1 SCAR=6.9
702 0 my ($self, $context) = @_;
703 0 $self->{_context} = lc($context);
704 0 $self->{_in_context} = undef;
705 0 $self;
706 }
707  
708 =item get_context
709  
710 Get the current search context.
711  
712 =cut
713  
714 0 ▶sub get_context {
Subroutine get_context: not called CC=1 SCAR=6.9
715 0 my ($self) = @_;
716 0 $self->{_context};
717 }
718  
719 =item result_in_context
720  
721 Get the context status of the last search.
722  
723 Empty means it was found out of context, a string indicates the
724 context in which the result was found, and undef indicates search
725 failure.
726  
727 =cut
728  
729 0 ▶sub result_in_context {
Subroutine result_in_context: not called CC=1 SCAR=6.9
730 0 my ($self) = @_;
731 0 $self->{_in_context};
732 }
733  
734 =item data [ I<start> ]
735  
736 Produces a Perl data structure created from all the properties from a
737 given point in the hierarchy.
738  
739 Note that since Perl hashes do not have an ordering, this information
740 will get lost. Also, properties can not have both a value and a substructure.
741  
742 =cut
743  
744 13 ▶sub data {
Subroutine data: called CC=1 SCAR=0.0
745 13 my ($self, $start) = ( @_, '' );
746 13 my $ret = $self->_data_internal($start);
747 13 $ret;
748 }
749  
750 672 ▶sub _data_internal {
Subroutine _data_internal: called CC=8 SCAR=20.8
751 672 my ( $self, $orig ) = @_;
752 672 ▶ my $cur = $orig // '';
Condition50%1/2
$orig // ""
l!l
6720
MC/DC0%0/1
$orig // ""
$orig
Truth table50%1/2
A
A= $orig
Aresult
11
00
753 672 ▶ $cur .= "." if $cur ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $cur ne "" 659 13 672
754 672 my $all = $cur;
755 672 $all .= '@';
756 672 ▶ if ( my $res = $self->{_props}->{lc($all)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $self->{'_props'}{lc $all}) { } 175 497 672
757 175 ▶ if ( _check_array($res) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_check_array($res)) { } 62 113 175
758 62 my $ret = [];
759 62 foreach my $prop ( @$res ) {
760 238 $ret->[$prop] = $self->_data_internal($cur.$prop);
761 }
762 62 return $ret;
763 }
764 else {
765 113 my $ret = {};
766 113 foreach my $prop ( @$res ) {
767 421 $ret->{$prop} = $self->_data_internal($cur.$prop);
768 }
769 113 return $ret;
770 }
771 }
772 else {
773 497 my $val = $self->{_props}->{lc($orig)};
774 497 ▶ $val = $self->expand($val) if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 485 12 497
775 497 return $val;
776 }
777 }
778  
779 175 ▶sub _check_array {
Subroutine _check_array: called CC=4 SCAR=13.9
780 175 my ( $i ) = @_;
781 175 my @i = @$i;
782 175 ▶ return unless "@i" =~ /^[\d ]+$/; # quick
Branch: 1 branch
BranchTrueFalseTotal
unless join($", @i) =~ /^[\d ]+$/ 113 62 175
783 62 my $ref = 0;
784 62 for ( @i) {
785 238 ▶ return unless $_ eq "$ref";
Branch: 1 branch
BranchTrueFalseTotal
unless $_ eq "$ref" 0 238 238
786 238 $ref++;
787 }
788 62 return 1; # success!
789 }
790  
791 =item dump [ I<start> [ , I<stream> ] ]
792  
793 Produces a listing of all properties from a given point in the
794 hierarchy and write it to the I<stream>.
795  
796 Without I<stream>, returns a string.
797  
798 In general, I<stream> should be UTF-8 capable.
799  
800 =item dumpx [ I<start> [ , I<stream> ] ]
801  
802 Like dump, but dumps with all values expanded.
803  
804 =cut
805  
806 13 my $dump_expanded;
807  
808 12 ▶sub dump {
Subroutine dump: called CC=2 SCAR=7.0
809 12 my ($self, $start, $fh) = ( @_, '' );
810 12 my $ret = $self->_dump_internal($start);
811 12 ▶ print $fh $ret if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 0 12 12
812 12 $ret;
813 }
814  
815 0 ▶sub dumpx {
Subroutine dumpx: not called CC=1 SCAR=6.9
816 0 my ($self, $start, $fh) = ( @_, '' );
817 0 $dump_expanded = 1;
818 0 my $ret = $self->dump( $start, $fh );
819 0 $dump_expanded = 0;
820 0 $ret;
821 }
822  
823 # internal
824  
825 347 ▶sub _dump_internal {
Subroutine _dump_internal: called CC=12 SCAR=24.9
826 347 my ($self, $cur) = @_;
827 347 ▶ $cur .= "." if $cur;
Branch: 1 branch
BranchTrueFalseTotal
if $cur 335 12 347
828 347 my $all = $cur;
829 347 $all .= '@';
830 347 my $ret = "";
831 347 ▶ if ( my $res = $self->{_props}->{lc($all)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $self->{"_props"}{lc $all}) 90 257 347
832 90 ▶ $ret .= "# $all = @$res\n" if @$res > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$res > 1 75 15 90
833 90 foreach my $prop ( @$res ) {
834 335 my $t = $self->_dump_internal($cur.$prop);
835 335 ▶ $ret .= $t if defined($t) && $t ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $t and $t ne "" 78 257 335
Condition66%2/3
defined $t and $t ne ""
!ll&&!rl&&r
025778
MC/DC50%1/2
defined $t and $t ne ""
defined $t $t ne ""
Truth table66%2/3
A and B
A= defined $tB= $t ne ""
ABresult
0X0
100
111
836 335 my $val = $self->{_props}->{lc($cur.$prop)};
837 335 ▶ $val = $self->expand($val) if $dump_expanded;
Branch: 1 branch
BranchTrueFalseTotal
if $dump_expanded 0 335 335
838 335 ▶ if ( !defined $val ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 89 246 335
elsif ($val =~ /[\n\t]/) { } 1 245 246
839 89 ▶ $ret .= "$cur$prop = null\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t and $t ne "" 12 77 89
Condition66%2/3
defined $t and $t ne ""
!ll&&!rl&&r
01277
MC/DC50%1/2
defined $t and $t ne ""
defined $t $t ne ""
Truth table66%2/3
A and B
A= defined $tB= $t ne ""
ABresult
0X0
100
111
840 unless defined($t) && $t ne '';
841 }
842 elsif ( $val =~ /[\n\t]/ ) {
843 1 $val =~ s/(["\\])/\\$1/g;
844 1 $val =~ s/\n/\\n/g;
845 1 $val =~ s/\t/\\t/g;
846 1 $ret .= "$cur$prop = \"$val\"\n";
847 }
848 else {
849 245 $val =~ s/(\\\')/\\$1/g;
850 245 $ret .= "$cur$prop = '$val'\n";
851 }
852 }
853 }
854 347 $ret;
855 }
856  
857 =for later
858  
859 package Tokenizer;
860  
861 sub new {
862 my ( $pkg, $lines ) = @_;
863 bless { _line => "",
864 _token => undef,
865 _lineno => 0,
866 _lines => $lines,
867 } => $pkg;
868 }
869  
870 sub next {
871 my ( $self ) = @_;
872 while ( $self->{_line} !~ /\S/ && @{$self->{_lines} } ) {
873 $self->{_line} = shift(@{ $self->{_lines} });
874 $self->{_lineno}++;
875 $self->{_line} = "" if $self->{_line} =~ /^\s*#/;
876 }
877 return $self->{_token} = undef unless $self->{_line} =~ /\S/;
878  
879 $self->{_line} =~ s/^\s+//;
880  
881 if ( $self->{_line} =~ s/^([\[\]\{\}=:])// ) {
882 return $self->{_token} = $1;
883 }
884  
885 # Double quoted string.
886 if ( $self->{_line} =~ s/^ " ((?>[^\\"]*(?:\\.[^\\"]*)*)) " //xs ) {
887 return $self->{_token} = qq{"$1"};
888 }
889  
890 # Single quoted string.
891 if ( $self->{_line} =~ s/^ ' ((?>[^\\']*(?:\\.[^\\']*)*)) ' //xs ) {
892 return $self->{_token} = qq{'$1'}
893 }
894  
895 $self->{_line} =~ s/^([^\[\]\{\}=:"'\s]+)//;
896 return $self->{_token} = $1;
897 }
898  
899 sub token { $_[0]->{_token } }
900 sub lineno { $_[0]->{_lineno } }
901  
902 =cut
903  
904 ################ Package End ################
905  
906 13 1;
907  
908 =back
909  
910 =head1 PROPERTY FILES
911  
912 Property files contain definitions for properties. This module uses an
913 augmented version of the properties as used in e.g. Java.
914  
915 In general, each line of the file defines one property.
916  
917 version: 1
918 foo.bar = blech
919 foo.xxx = yyy
920 foo.xxx = "yyy"
921 foo.xxx = 'yyy'
922  
923 The latter three settings for C<foo.xxx> are equivalent.
924  
925 Whitespace has no significance. A colon C<:> may be used instead of
926 C<=>. Lines that are blank or empty, and lines that start with C<#>
927 are ignored.
928  
929 Property I<names> consist of one or more identifiers (series of
930 letters and digits) separated by periods.
931  
932 Valid values are a plain text (whitespace, but not trailing, allowed),
933 a single-quoted string, or a double-quoted string. Single-quoted
934 strings allow embedded single-quotes by escaping them with a backslash
935 C<\>. Double-quoted strings allow common escapes like C<\n>, C<\t>,
936 C<\7>, C<\x1f> and C<\x{20cd}>.
937  
938 Note that in plain text backslashes are taken literally. The following
939 alternatives yield the same results:
940  
941 foo = a'\nb
942 foo = 'a\'\nb'
943 foo = "a'\\nb"
944  
945 B<IMPORTANT:> All values are strings. These three are equivalent:
946  
947 foo = 1
948 foo = "1"
949 foo = '1'
950  
951 and so are these:
952  
953 foo = Hello World!
954 foo = "Hello World!"
955 foo = 'Hello World!'
956  
957 Quotes are required when you want leading and/or trailing whitespace.
958 Also, the value C<null> is special so if you want to use this as a string
959 it needs to be quoted.
960  
961 Single quotes defer expansion, see L</"Expansion"> below.
962  
963 =head2 Context
964  
965 When several properties with a common prefix must be set, they can be
966 grouped in a I<context>:
967  
968 foo {
969 bar = blech
970 xxx = "yyy"
971 zzz = 'zyzzy'
972 }
973  
974 Contexts may be nested.
975  
976 =head2 Arrays
977  
978 When a property has a number of sub-properties with keys that are
979 consecutive numbers starting at C<0>, it may be considered as an
980 array. This is only relevant when using the data() method to retrieve
981 a Perl data structure from the set of properties.
982  
983 list {
984 0 = aap
985 1 = noot
986 2 = mies
987 }
988  
989 When retrieved using data(), this returns the Perl structure
990  
991 [ "aap", "noot", "mies" ]
992  
993 For convenience, arrays can be input in several more concise ways:
994  
995 list = [ aap noot mies ]
996 list = [ aap
997 noot
998 mies ]
999  
1000 The opening bracket must be followed by one or more values. This will
1001 currently not work:
1002  
1003 list = [
1004 aap
1005 noot
1006 mies ]
1007  
1008 =head2 Includes
1009  
1010 Property files can include other property files:
1011  
1012 include "myprops.prp"
1013  
1014 All properties that are read from the file are entered in the current
1015 context. E.g.,
1016  
1017 foo {
1018 include "myprops.prp"
1019 }
1020  
1021 will enter all the properties from the file with an additional C<foo.>
1022 prefix.
1023  
1024 =head2 Expansion
1025  
1026 Property values can be anything. The value will be I<expanded> before
1027 being assigned to the property unless it is placed between single
1028 quotes C<''>.
1029  
1030 Expansion means:
1031  
1032 =over
1033  
1034 =item *
1035  
1036 A tilde C<~> in what looks like a file name will be replaced by the
1037 value of C<${HOME}>.
1038  
1039 =item *
1040  
1041 If the value contains C<${I<name>}>, I<name> is first looked up in the
1042 current environment. If an environment variable I<name> can be found,
1043 its value is substituted.
1044  
1045 If no suitable environment variable exists, I<name> is looked up as a
1046 property and, if it exists and has a non-empty value, this value is
1047 substituted.
1048  
1049 Otherwise, the C<${I<name>}> part is removed.
1050  
1051 Note that if a property is referred as C<${.I<name>}>, I<name> is
1052 looked up in the current context only.
1053  
1054 B<Important:> Property lookup is case insensitive, B<except> for the
1055 names of environment variables B<except> on Microsoft Windows
1056 where environment variable names are looked up case insensitive.
1057  
1058 =item *
1059  
1060 If the value contains C<${I<name>:I<value>}>, I<name> is looked up as
1061 described above. If, however, no suitable value can be found, I<value>
1062 is substituted.
1063  
1064 =back
1065  
1066 Expansion is delayed if single quotes are used around the value.
1067  
1068 x = 1
1069 a = ${x}
1070 b = "${x}"
1071 c = '${x}'
1072 x = 2
1073  
1074 Now C<a> and C<b> will be C<'1'>, but C<c> will be C<'2'>.
1075  
1076 Substitution is handled by L<String::Interpolate::Named>. See its
1077 documentation for more power.
1078  
1079 In addition, you can test for a property being defined (not null) by
1080 appending a C<?> to its name.
1081  
1082 result = ${x?|${x|value|empty}|null}
1083  
1084 This will yield C<value> if C<x> is not null and not empty, C<empty>
1085 if not null and empty, and C<null> if not defined or defined as null.
1086  
1087 =head1 SEE ALSO
1088  
1089 L<File::LoadLines>, L<String::Interpolate::Named>.
1090  
1091 =head1 BUGS
1092  
1093 Although in production for over 25 years, this module is still
1094 slightly experimental and subject to change.
1095  
1096 =head1 AUTHOR
1097  
1098 Johan Vromans, C<< <JV at cpan.org> >>
1099  
1100 =head1 SUPPORT AND DOCUMENTATION
1101  
1102 Development of this module takes place on GitHub:
1103 https://github.com/sciurius/perl-Data-Properties.
1104  
1105 You can find documentation for this module with the perldoc command.
1106  
1107 perldoc Data::Properties
1108  
1109 Please report any bugs or feature requests using the issue tracker on
1110 GitHub.
1111  
1112 =head1 ACKNOWLEDGEMENTS
1113  
1114 This module was initially developed in 1994 as part of the Multihouse
1115 MH-Doc (later: MMDS) software suite. Multihouse kindly waived copyrights.
1116  
1117 In 2002 it was revamped as part of the Compuware OptimalJ development
1118 process. Compuware kindly waived copyrights.
1119  
1120 In 2020 it was updated to support arrays and released to the general
1121 public.
1122  
1123 =head1 COPYRIGHT & LICENSE
1124  
1125 Copyright 1994,2002,2020 Johan Vromans, all rights reserved.
1126  
1127 This program is free software; you can redistribute it and/or modify it
1128 under the same terms as Perl itself.
1129  
1130 =cut
1131  
1132 13 1; # End of Data::Properties
« lib/ChordPro/lib/SVGPDF/Use.pm ↑ Summary blib/lib/ChordPro/Wx/Config.pm »