line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Config-Model |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is Copyright (c) 2005-2022 by Dominique Dumont. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software, licensed under: |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# The GNU Lesser General Public License, Version 2.1, February 1999 |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
package Config::Model::Role::ComputeFunction 2.153; # TRIAL |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# ABSTRACT: compute &index or &element functions |
13
|
|
|
|
|
|
|
|
14
|
59
|
|
|
59
|
|
34638
|
use Mouse::Role; |
|
59
|
|
|
|
|
216
|
|
|
59
|
|
|
|
|
597
|
|
15
|
59
|
|
|
59
|
|
22817
|
use strict; |
|
59
|
|
|
|
|
199
|
|
|
59
|
|
|
|
|
1423
|
|
16
|
59
|
|
|
59
|
|
390
|
use warnings; |
|
59
|
|
|
|
|
189
|
|
|
59
|
|
|
|
|
2033
|
|
17
|
59
|
|
|
59
|
|
434
|
use Carp; |
|
59
|
|
|
|
|
198
|
|
|
59
|
|
|
|
|
3954
|
|
18
|
|
|
|
|
|
|
|
19
|
59
|
|
|
59
|
|
492
|
use Mouse::Util; |
|
59
|
|
|
|
|
227
|
|
|
59
|
|
|
|
|
563
|
|
20
|
59
|
|
|
59
|
|
5525
|
use Log::Log4perl qw(get_logger :levels); |
|
59
|
|
|
|
|
213
|
|
|
59
|
|
|
|
|
725
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $logger = get_logger("ComputeFunction"); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub compute_string { |
25
|
136
|
|
|
136
|
1
|
395
|
my ($self, $string, $check) = @_; |
26
|
136
|
|
|
|
|
674
|
$string =~ s/&(index|element)(?:\(([- \d])\))?/$self->eval_function($1,$2,$check)/eg; |
|
159
|
|
|
|
|
462
|
|
27
|
136
|
|
|
|
|
581
|
return $string; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub eval_function { |
31
|
179
|
|
|
179
|
1
|
745
|
my ($self, $function, $up, $check) = @_; |
32
|
|
|
|
|
|
|
|
33
|
179
|
100
|
|
|
|
501
|
if (defined $up) { |
34
|
|
|
|
|
|
|
# get now the object referred |
35
|
124
|
|
|
|
|
276
|
$up =~ s/\s//g; |
36
|
124
|
|
|
|
|
218
|
$up =~ s/-(\d+)/'- ' x $1/e; # change -3 -> - - - |
|
2
|
|
|
|
|
9
|
|
37
|
124
|
|
|
|
|
512
|
$up =~ s/(-+)/'- ' x length($1)/e; # change --- -> - - - |
|
124
|
|
|
|
|
468
|
|
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
179
|
|
|
|
|
348
|
my $target = eval { |
41
|
179
|
100
|
|
|
|
639
|
defined $up ? $self->grab( step => $up, check => $check ) : $self; |
42
|
|
|
|
|
|
|
}; |
43
|
|
|
|
|
|
|
|
44
|
179
|
50
|
|
|
|
416
|
if ($@) { |
45
|
0
|
|
|
|
|
0
|
my $e = $@; |
46
|
0
|
0
|
0
|
|
|
0
|
my $msg = ref($e) && $e->can('full_message') ? $e->full_message : $e; |
47
|
0
|
|
|
|
|
0
|
Config::Model::Exception::Model->throw( |
48
|
|
|
|
|
|
|
object => $self, |
49
|
|
|
|
|
|
|
error => "Compute function argument '$up':\n" . $msg |
50
|
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
179
|
|
|
|
|
249
|
my $result ; |
54
|
179
|
100
|
|
|
|
455
|
if ( $function eq 'element' ) { |
|
|
50
|
|
|
|
|
|
55
|
114
|
|
|
|
|
316
|
$result = $target->element_name; |
56
|
114
|
50
|
|
|
|
258
|
Config::Model::Exception::Model->throw( |
57
|
|
|
|
|
|
|
object => $self, |
58
|
|
|
|
|
|
|
error => "Compute function error: '". $target->name. "' has no element name" |
59
|
|
|
|
|
|
|
) unless defined $result; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
elsif ( $function eq 'index' ) { |
62
|
65
|
|
|
|
|
191
|
$result = $target->index_value; |
63
|
65
|
50
|
|
|
|
208
|
Config::Model::Exception::Model->throw( |
64
|
|
|
|
|
|
|
object => $self, |
65
|
|
|
|
|
|
|
error => "Compute function error: '". $target->name. "' has no index value" |
66
|
|
|
|
|
|
|
) unless defined $result; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else { |
69
|
0
|
|
|
|
|
0
|
Config::Model::Exception::Model->throw( |
70
|
|
|
|
|
|
|
object => $self, |
71
|
|
|
|
|
|
|
error => "Unknown compute function &$function, " |
72
|
|
|
|
|
|
|
. "expected &element(...) or &index(...)" |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
179
|
|
|
|
|
882
|
return $result; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
__END__ |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=pod |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=encoding UTF-8 |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 NAME |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Config::Model::Role::ComputeFunction - compute &index or &element functions |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 VERSION |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
version 2.153 |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head1 SYNOPSIS |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
$value->eval_function('index'); |
98
|
|
|
|
|
|
|
$value->eval_function('element'); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
$value->eval_function('index','-'); |
101
|
|
|
|
|
|
|
$value->eval_function('index','- -'); |
102
|
|
|
|
|
|
|
$value->eval_function('index','-3'); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
$value->compute_string('&element(-)') |
105
|
|
|
|
|
|
|
$value->compute_string('&index(- -)'); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 DESCRIPTION |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Role used to let a value object get the index or the element name of |
110
|
|
|
|
|
|
|
C<$self> or of a node above. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 METHODS |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 eval_function |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Retrieve the index or the element name. Parameters are |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
( function_name , [ up ]) |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=over |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item function_name |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
C<element> or C<index> |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item up |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Optional parameter to indicate how many level to go up before |
129
|
|
|
|
|
|
|
retrieving the index or element name. Each C<-> is equivalent to a |
130
|
|
|
|
|
|
|
call to C<parent|Config::Model::Node/parent>. Can be repeated dashes |
131
|
|
|
|
|
|
|
("C<->", "C<- ->", ...) |
132
|
|
|
|
|
|
|
or a dash with a multiplier |
133
|
|
|
|
|
|
|
("C<->", "C<-2>", ...). White spaces are ignored. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=back |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 compute_string |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Perform a similar function as C<eval_function> using a string where |
140
|
|
|
|
|
|
|
function names are extracted. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
E.g. C<compute_string('&element(-)')> calls C<eval_function('element','-')> |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 AUTHOR |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Dominique Dumont |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
This software is Copyright (c) 2005-2022 by Dominique Dumont. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
This is free software, licensed under: |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
The GNU Lesser General Public License, Version 2.1, February 1999 |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=cut |